Android Programming – Activity life Cycle Development Questions and Answers

Q-1 Draw a diagram for activity life cycle and explain it.

  • To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks:

onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy().

  • The system invokes each of these callbacks as an activity enters a new state.
  • As the user begins to leave the activity, the system calls methods to dismantle the activity.
  • In some cases, this dismantlement is only partial; the activity still resides in memory (such as when the user switches to another app), and can still come back to the foreground.
  • If the user returns to that activity, the activity resumes from where the user left off
  • The system’s likelihood of killing a given process—along with the activities in it—depends on the state of the activity at the time.
  • Activity state and ejection from memory provides more information on the relationship between state and vulnerability to ejection
  • Depending on the complexity of your activity, you probably don’t need to implement all the lifecycle methods.

 

Lifecycle Methods

 

onCreate()

  • You must implement this callback, which fires when the system first creates the activity.
  • On activity creation, the activity enters the Created state.
  • In the onCreate() method, you perform basic application startup logic that should happen only once for the entire life of the activity.
  • For example, your implementation of onCreate() might bind data to lists, initialize background threads, and instantiate some class-scope variables.
  • This method receives the parameter savedInstanceState, which is a Bundle object containing the activity’s previously saved state.
  • If the activity has never existed before, the value of the Bundle object is null.

 

Your activity does not reside in the Created state. After the onCreate() method finishes execution, the activity enters the Started state, and the system calls the onStart() and onResume() methods in quick succession.

 

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
  • For example, this method is where the app initializes the code that maintains the UI.
  • 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.

 

onResume()

  • When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume() callback.
  • 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 an interruptive event occurs, the activity enters the Paused state, and the system invokes the onPause() callback
  • If the activity returns to the Resumed state from the Paused state, the system once again calls onResume() method.
  • For this reason, you should implement onResume() to initialize components that you release during onPause().

 

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).
  • Use the onPause() method to pause operations such animations and music playback that should not continue while the Activity is in the Paused state, and that you expect to resume shortly.
  • You can use the onPause() method to release system resources, such as broadcast receivers, 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
  • For this reason, you should not use onPause() 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().
  • If the activity resumes, the system once again invokes the onResume() callback.
  • If the activity returns from the Paused state to the Resumed state, the system keeps the Activity instance resident in memory, recalling that instance when it the system invokes onResume().

 

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 almost all resources that aren’t needed while the user is not using it.
  • When your activity enters the Stopped state, the Activity object is kept resident in memory: It maintains all state and member information, but is not attached to the window manager
  • When the activity resumes, the activity recalls this information.
  • 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 object 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

 

onDestroy()

 

  • Called before the activity is destroyed.
  • This is the final call that the activity receives.
  • The system either invokes this callback because the activity is finishing due to someone’s calling finish(), or because the system is temporarily destroying the process containing the activity to save space.
  • You can distinguish between these two scenarios with the isFinishing() method.
  • The system may also call this method when an orientation change occurs, and then immediately call onCreate() to recreate the process (and the components that it contains) in the new orientation.
  • The onDestroy() callback releases all resources that have not yet been released by earlier callbacks such as onStop().