Android activity Lifecycle

Abhishek Kumar Sah
6 min readJun 10, 2019

--

An activity is a single, focused thing that the user can do. An app constitutes of activities with other components. The Activity class provides a number of callbacks that allow the activity to know that a state has changed. They are in sequence:-

  1. onCreate( )
  2. onStart( )
  3. onRestoreInstanceState( )
  4. onResume( )
  5. onPause( )
  6. onSaveInstanceState( )
  7. onStop( )
  8. onRestart( )
  9. onDestroy( )

Good implementation of the lifecycle callbacks can help ensure that your app avoids:

  • Crashing if the user receives a phone call or switches to another app while using your app.
  • Consuming valuable system resources when the user is not actively using it.
  • Losing the user’s progress if they leave your app and return to it at a later time.
  • Crashing or losing the user’s progress when the screen rotates between landscape and portrait orientation.

onCreate( )

This callback is fired when the system first creates the activity. In onCreate method, all such operations are performed which should be done only once for the entire life of the activity.

This method has a parameter savedInstanceState which is a bundle object. It contains activity’s previous saved state. If the activity has never existed before, the value of Bundle object is null.

onStart( )

When the activity enters the Started state, the system invokes this callback. The onStart( ) call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive. This method can be used to initialize the code that maintains the user interface.

After onStop( ) , if activity restarts then onStart( ) is called. onCreate ( ) function is called only once but onStart( ) can be called multiple times , when activity enters the started state.

onRestoreInstanceState( )

When your activity is recreated after it was previously destroyed, you can recover your saved instance state from the bundle that the system passes to your activity. Both the onCreate( ) and onRestoreInstanceState( ) callback methods receive the same bundle that contains the instance state information.

onResume( )

The onStart( ) method completes very quickly and, as with the Created state, the activity does not stay resident in the Started state. Once this callback finishes, the activity enters the Resumed state, and the system invokes the onResume( ) method.

This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app. Such an event might be, for instance, receiving a phone call, the user’s navigating to another activity, or the device screen’s turning off. When such an event occurs, the activity enters the Paused state, and the system invokes the onPause( ) callback.

onPause( )

The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed); it indicates that the activity is no longer in the foreground (though it may still be visible if the user is in multi-window mode).

When the activity moves to the paused state, any lifecycle-aware component tied to the activity’s lifecycle will receive the onPause( ) event. This is where the lifecycle components can stop any functionality that does not need to run while the component is not in the foreground, such as stopping a camera preview.

onPause( ) method can be used to release system resources, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them. onPause( ) execution is very brief, and does not necessarily afford enough time to perform save operations. So onPause( ) should not be used to save application or user data, make network calls, or execute database transactions; such work may not complete before the method completes. Instead, you should perform heavy-load shutdown operations during onStop( ).

onSaveInstanceState( )

As your activity begins to stop, the system calls the onSaveInstanceState( ) method so your activity can save state information to an instance state bundle.

The default implementation of this method saves transient information about the state of the activity’s view hierarchy, such as the text in an EditText widget or the scroll position of a Listview widget.

To save additional instance state information for your activity, you must override onSaveInstanceState( ) and add key-value pairs to the bundle object that is saved in the event that your activity is destroyed unexpectedly.

onStop( )

When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop( ) callback. This may occur, for example, when a newly launched activity covers the entire screen. The system may also call onStop( ) when the activity has finished running, and is about to be terminated.

In the onStop( ) method, the app should release or adjust resources that are not needed while the app is not visible to the user. It should be used to perform relatively CPU-intensive shutdown operations. If we can’t find a more opportune time to save information to a database, we might do so during onStop( )

onRestart( )

onStart( ) will always be called whenever you enter your Activity just after onCreate( ) but the onRestart( ) will only be called before onStart( ) when your Activity comes from being stopped (passing from onStop( )) back to the vision.

onDestroy( )

onDestroy( ) is called before the activity is destroyed. The system invokes this callback either because:

  1. the activity is finishing (due to the user completely dismissing the activity or due to finish( ) being called on the activity), or
  2. the system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode)

If the activity is finishing, onDestroy() is the final lifecycle callback the activity receives. If onDestroy() is called as the result of a configuration change, the system immediately creates a new activity instance and then calls onCreate( ) on that new instance in the new configuration.

After reading the above description of android lifecycle callbacks, the following chart concludes all.

📝 Read this story later in Journal.

👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.

--

--

Abhishek Kumar Sah

Android Developer | Open Source Enthusiast | Writer at NoteWorthy — The Journal Blog