Explain the life cycle of an activity, in android?

Answer :  The life cycle of Activity as below

onCreate-> onStart() -> onResume() -> onPause() -> onStop() -> onDestroy()

Explanation for android activity life cyle:

onCreate()

1.The system calls this when creating your activity.
2.You should initialize the essential components of your activity.
Eg:setcontentview
3.Using findviewbyid(), to programmatically interact with widgets in the UI
4.calling managedQuery
5.You can call finish()
6.If you want any thread running in the background to download DATA, you can   do it here. This has to be deleted in onDestroy()
7.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

onStart()
1.Called after onCreate(Bundle)  or after onRestart() followed by onResume().
2.you can register a BroadcastReceiver in onStart() to monitor changes that impact your UI, You hav to unregister it in onStop()
3.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

onResume()
1.Called after onRestoreInstanceState(Bundle), onRestart(), or onPause()
2.begin animations, open exclusive-access devices (such as the camera)

onPause ()
1.Called as part of the activity lifecycle when an activity is going into the background
2.The counterpart to onResume().
3.B will not be created until A's onPause() returns. (So don’t do anything long here)
4.Save all your persistent data here.
5.Good place to do things like stop animations (any CPU consuming things), to make switching to other activity as soon as possible.
6.Close your camera here if it is opened
7.Eg: if device goes to sleep or some dialog is displayed, then it will go to onPause().
8.onStop() will follow this function, once the other waiting activity resumes and started displaying on the scree. This is not guaranteed as in some cases onResume() will be called with out onStop().
9.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

onStop ()
1.Called when you are no longer visible to the user
2.You will next receive either onRestart(), onDestroy()
3.this method may never be called, in low memory situations
4.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

onDestroy ()
1.Perform any final cleanup before an activity is destroyed
2.do not count on this method being called as a place for saving data!
3.This method is usually implemented to free resources like threads that are associated with an activity,
4.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown

onRestart ()
1.Called after onStop() when the current activity is being re-displayed to the user
2.It will be followed by onStart() and then onResume()
3.If you have deactivated any cursor in onStop(), call managedQuery() again.
4.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

onSaveInstanceState – this is where we have to save transient state of the application
onRestoreInstanceState – this is where we have to restore the transient state of our application that has been crashed due to low memory scenarios.


No comments:

Post a Comment