Friday, 5 July 2013

LIFECYCLE of an ACTIVITY (Part 1)

Hello Friends,
            Welcome you all in the Android Tutorial.



Today we are going to learn about the very important component of android application “ACTIVTY” and it’s LIFECYCLE.

Like any other programming language like C, C++ or Java where the execution of the code starts from the main() function.
In android same thing can be achieved by the ACTIVITY. In android application main activity is behave like main() function.

What is an Activity?

An Activity provides a user interface, from where the user can interact with our android application.
Each activity is a single screen. An android application can have multiple activities.
            Main activity is same like any other activity but main activity is the one who interacts with the user first time when they start the application.
An activity can start another activity also in order to perform different actions.

Lifecycle of an Activity:-
           
         The following diagram shows the Lifecycle of an Activity:-



The key parts of the lifecycle are the “system calls”.
With the help of these system calls, activity changes his state from one to another.
There are only three main states of the Lifecycle, which can be static.
They are:-
1)    Resumed
2)    Paused
3)    Stopped

Resumed:-
In this state an activity is visible to the user. User can interact with the activity at this time. We can also refer this state as a “Running” state.

Paused:-
Suppose activity A is in Resumed/Running state. At this time another activity B came up in the Running state. Activity B has not covered the entire screen or we can say activity B is semi-transparent.

            This time activity A moved into the Paused state.
In Paused State, activity A does not interact with the user and cannot execute any code.

Stopped:-
            In this state activity is completely hidden or not visible to the user.
            We can say activity is running in the background.
In this state activity instance and its state information like member variables are retained.
In this state also activity cannot execute any code.

When user first time launches the application, the system create the new instance of the Activity by calling onCreate() method.
Once onCreate() finishes execution, the system calls the onStart() and onResume()  method and the Activity moved into the Resumed/Running State.

onCreate() is the very first lifecycle callback and onDestroy() is the very last.

It is not necessary to implement all the lifecycle methods. It’s all depend on the requirement of the application what we develop.




In the next part of ACTIVITY LIFECYCLE, we will be writing some code and trying to understand how to start an Activity and how to move from one state to another.





Thanks for reading, please give your suggestion.

Please share it !!!