Slide show

Total Pageviews

Powered by Blogger.

Translate

Search This Blog

Pages

Android Activity Life Cycle

                                   Android Activity Life Cycle Tutorial


Hi,today i'll explain you the life cycle of android activity.Each and every activity in android maintains it's own life cycle.Android system maintains the life cycle of each activity. For understanding the life cycle of activity we have to cover three different conditions :



First condition is :-

User launches an application having one single activity, and after successful launch of the activity, user presses the back button of the device. So, in this situation the following methods of Activity Class will participate in lifecycle process that gets called for the launch of the activity are :

onCreate(Bundle saveState){ }
onStart(){ }
onResume(){ }

Now user can see and interact with the activity.

After that user presses the back button of the device. Now in that case activity is going to be killed by Android system and all the resources deallocation process will be done.

Methods called for this are:

onPause(){ }
onStop(){ }
onDestroy(){ }


Second Condition is :-
In second condition user launches the application by pressing the icon. The activity is on the front of user. So for this following methods gets called :

onCreate(Bundle saveState){ }
onStart(){ }
onResume(){ }

Now the activity is visible to user and user can interact with the activity.
Now there is a button on the first activity. By pressing the button user will be navigated to second activity. So in this condition android system will call following methods for the first activity :

onPause(){ }
onStop(){ }

Now user is on second activity.
After that from second activity user presses the back button of the device,so user will be navigated back to first activity. Now while again displaying the activity on the screen android will call following methods for the first activity :

onRestart(){ }
onStart(){ }
onResume(){ }

As now user is on first activity so when he again presses the back button following methods gets called :

onPause(){ }
onStop(){ }
onDestroy(){ }
Third Condition is:-


In third condition user presses the application icon to launch the application. This time our second activity is transparent. On the first activity there is a text and a button on the activity. On launch of first activity following methods get called :
onCreate(Bundle saveState){ }
onStart(){ }
onResume(){ }

Now the activity is visible to user and user can interact with the activity.
Now there is a button on the first activity. By pressing the button user will be navigated to second activity. As already mentioned the second activity is transparent so this time following methods gets called for the first activity :

onPause(){ }
Now user is on second activity. User can also see the UI content of the first activity behind the second activity but he can not interact with the components of activity.

After being on second activity now user presses the back button. So he will again move back to the first activity.
This time following methods gets called for the first activity :
onResume(){ }

As now user is on first activity. So when he again presses the back button following methods gets called :

onPause(){ }
onStop(){ }
onDestroy(){ }




Additional Condition:

We have one additional condition, which is user launches the application by pressing the application icon, and user can see the first launching activity. The methods gets called will be :


onCreate(Bundle saveState){ }
onStart(){ }
onResume(){ }

Now this time user presses the Home button of the device. So, in this conditions the methods gets called will be :


onPause(){ }
onStop(){ }

This time if android system had enough memory, then the instance of activity will not be destroyed.

Now user again launches the application. Now system will call following methods for the activity :


onRestart(){ }
onStart(){ }
onResume(){ }


As now user is on activity. So when he again presses the back button following methods gets called :

onPause(){ }
onStop(){ }
onDestroy(){ }






**IMPORTANT NOTE :-

Calling of onStop(){ } method is not sure every time during the lifecycle process of an activity. Its Behavior changes with availability of device memory.If Device is in Critical Low memory condition then it just overpasses the  onStop(){ }  method and directly calls the onDestroy() { } method directly after onPause(){ } method call. Thats Why it is Recommended that you should do Application closing time operation in onPause(){ } & not in onStop(){ }. 




No comments:

vehicles

business

health