top of page
  • Writer's pictureMeghan Gill

Activity Lifecycle in Android


In Android, activities are managed using an activity stack. When a new activity is started, it’s placed on top of the stack and becomes the running activity.

Activities Can Exist in 3 States:

  • Running - Activity is started and on the top of the activity stack.

  • Paused - Activity is visible but not on top of the activity stack.

  • Stopped - An activity can be destroyed by the operating system.


7 Activity Lifecycle Methods





Activity Launched - 3 Methods called:


onCreate( ) - performs any initial start up procedures. It controls initializing variables, creating views, or starting background processes. It also provides a bundle containing the activity’s previous instance state, if applicable, or a null bundle.

onStart( ) - represents the point in the app’s activity lifecycle right before it’s visible to the user and not yet on top of the activity stack. That means the user can’t yet interact with it. onStart( ) method is rarely overridden.

onResume( ) - is called immediately after onStart( ). It often starts any animations in our activity. Once onResume( ) returns, the activity is running. At this point, the activity is finally on top of the activity stack and can interact with the user.



Activity Running


The activity is now up and running, but all things come to an end.



 


2 Possible Directions


What happens now depends on the button pressed - the BACK button or a different button results in the following methods being called:



onPause( ) - is called when another activity is taking over the top of the activity stack and is becoming the foreground activity. We can commit unsaved changes and stop animations here.


Can our activity be seen in the background?





Yes - When the foreground activity is dismissed our activity calls

onResume( ).

No - If the new activity completely obscures our activity, then onStop( ) is called.



onStop( ) is called if an activity enters the stopped state and is no longer visible. It’s rare that onStop is overridden. Services and broadcast receivers are a good reason to override onStop( ) and onStart( ).

Did the user return to our activity?

Yes - onRestart( ) is called. onRestart( ) is hardly ever overridden. Then onStart( ) is called.

No - onDestroy( ) is called.



onDestroy( ) - the user has exited the activity by using the back button or in a way that has explicitly called the activity’s finish( ) method, or because the activity had been stopped and is now being destroyed by the operating system to reclaim memory.


onDestroy( ) is typically used to clean up background processes started in onCreate( ).





89 views0 comments

Recent Posts

See All
bottom of page