When I read Android Training , I saw a description for the activity-lifecycle:
When your activity is stopped, the Activity object is kept resident in memory and is recalled when the activity resumes. You don’t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state. The system also keeps track of the current state for each View in the layout, so if the user entered text into an EditText widget, that content is retained so you don't need to save and restore it.
I'm confused, if I don't need to re-initialize any components, why do I have to handle data or any something on callback method?
Just take Camera as an example.
You don't need to reinitialize any component, but you need to reinitialize the camera when the activity resumed. Component arne't the only part of an activity. Most applications also need to access system resources. You release these resources when your application gets paused or stopped, so you must reinitialize them again when your application is resumed. And Android may destroy your application in some circumstances (memory not enough, etc.) So you have to release and reinitialize resources again.
Moreover, there might be some internal state of your application. For example a book reader. You need to record the progress. This internal state are not components but you have to save them when your activity get paused.
The following code is sourced from Android documentation. Old mirror available here:
@Override public void onPause() { super.onPause(); // Always call the superclass method first // Release the Camera because we don't need it when paused // and other activities might need to use it. if (mCamera != null) { mCamera.release() mCamera = null; } } @Override public void onResume() { super.onResume(); // Always call the superclass method first // Get the Camera instance as the activity achieves full user focus if (mCamera == null) { initializeCamera(); // Local method to handle camera init } }