This is the most important and basic concept in Android. In Android, every activity has its on life cycle. An activity encountered the following methods in its life cycle.
1. onCreate()
2. onStart()
3. onResume()
4. onPause()
5. onStop()
6. onResume()
7. onDestroy()
1. onCreate()
2. onStart()
3. onResume()
4. onPause()
5. onStop()
6. onResume()
7. onDestroy()
1. onCreate(): This method is called only once in an activity life cycle. onCreate() method is called when an activity is created for the first time. This is the main method where we initialize all the variables.
In onCreate() method, a variable of class Bundle is passed. This helps to save the state of our activity or application.
It comes in the role when the orientation of an activity is changed. A very basic example is, when a user is filling online form in android application and changes its orientation from portrait to landscape or vice versa then Bundle class helps the application in retaining the user data.
2. onStart(): onStart() method is called when an application or activity becomes visible to the user. It is usually used to reset activity data or re-initializing the variables. In entire life cycle of an activity, onStart() method possible be called twice i.e. after onCreate() and onRestart().
3. onResume(): Android calls onResume() method when an activity becomes visible to the user. In this state user can interact when the application.
4. onPause(): It is called when a new activity comes above the current activity. In this case current activity gets in pause state. when onPause() is called an activity becomes invisible or partially visible to the user.
Usually onPause() method is called to save the state of the current activity, so that ther ewill be no data loose when any other application interrupts your application.
5. onStop(): When onStop() method is called our application becomes invisible to the user. it is also called to save the current state of application.
There is a thin line between onPasue() and onStop(). when another activity comes to the foreground then our activity is in Pause and Stop both the states. but when any text message or any alert message appears on screen then it becomes partially visible and called onPause() method.
6. onRestart(): This is quite similar to the onCreate() method. But we usually called this method to see our hidden activity once again. After calling onStop(), android adds the activity into the activity stack and another activity comes in to the foreground. But when user intentionally calls up any activity which is in activity stack, then onRestart() method is called.
7. onDestroy(): On calling onDestroy() method, the activity gets killed. This could be possible in only two cases. one if user shutdowns the entire application and second when user manually calls "finish()" method.
Once the onDestory method comes into role then for starting the application android has to start the entire life cycle from beginning.
Comments
Post a Comment