Home  >  Android

Android Activity Life Cycle Example

By Arvind Rai, October 12, 2014
On this page we will understand the life cycle of Android Activity. Activity is a component that plays the role to show the UI with which user interacts. User interacts with UI for dialing a phone, opening URL, taking picture etc. At this stage, activity is in foreground. Each activity opens a window and callas another activity to navigate another window and now this is in foreground and previous activity is paused or stopped. Find the below picture that is showing the sequence of different methods in activity life cycle.
Android Activity Life Cycle Example

Android Activity States

In the life cycle of Android Activity, it can remain in three states.
Resumed: This is the running state of android activity. The activity will be running in the foreground.
Paused : This activity is not running but visible in foreground. Other activity will be running in foreground. Paused activity can be killed by system if memory required.
Stopped : This state of activity is also live as paused state. But stopped state is not visible in foreground. It can also be killed if memory is needed.

Skeleton of Android Activity Life Cycle

The android.app.Activity class has different methods that handle the life cycle. Find the Skelton of overriding skeleton methods.
public class ActivityDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //TODO
    }
    @Override
    protected void onStart() {
        super.onStart();
        //TODO
    }
    @Override
    protected void onResume() {
        super.onResume();
        //TODO
    }
    @Override
    protected void onPause() {
        super.onPause();
        //TODO
    }
    @Override
    protected void onStop() {
        super.onStop();
        //TODO
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //TODO
    }
} 
 
Now find the short description of methods of android life cycle.
onCreate() : When the activity is first created, onCreate() method is called. In this method we perform view creation and data binding.
onStart(): After activity creation, it becomes eligible to run on foreground, so before running, onStart() method is called.
onRestart(): If an activity has stopped and going to restart, then just before restart , onRestart() method is called.
onResume(): This is the point when an activity interacts with the user. onResume() method is called just before activity starts interacting with the user.
onPause(): When an activity starts another activity, after that first activity is stopped. So onPause() method is called for an activity when that is going to start another activity.
onStop(): If an activity is going to stop, then just before stop, onStop() method is called.
onDestroy(): Before activity is destroyed, onDestroy() method is called.
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us