Pages

Saturday, May 21, 2016

Activity Life Cycle

Android Activity Life Cycle
                                           An Activity is usually a single screen that the users sees on the device at a one time. Any application that has been developed by anyone it typically contains multiple activities.

* Most management of the life cycle is done automatically by the system via the activity Stack

* Activity manager is responsible for creating , destroying and managing activities.

For Example when the user starts an application for the first time, the activity manager will create
its activity and put it into the screen. later, when  the user navigating to different screens . The activity manager will move to the previous activity to a holding place .This way if the user wants to go back to older activity , it can be started more quickly , older activities that the user has not used in a while will be destroyed in order to free  more space fort the currently one .

In Activity Life Cycle we are having following methods:
                 1. onCreate()
                 2. onStart()
                 3. onRestart()
                 4. onResume()
                 5. onPause()
                 6. onStop()
                 7. onDestroy()

1. onCreate(); Called when the activity is first created. this is where you should do all  of your normal static setup. create views bind data to lists etc, this method also provides you with a bundle containing the activities previous state.

2. onStart(): Called when the activity become visible to the user.

3. onRestart(); called after your activity has been stopped. prior to it being started again

4. onResume(); Called when the activity starts interacting with the user. At this point your activity is at the top of the stack.

5. onPause(): Called when the system is about to start resuming a previous activity. this is typically used to commit unsaved changes to persistent data, stop Animations and graphics that may be consuming CPU .

6. onStop(): Called when the activity is no longer visible to the user. and another activity has been resumed and is covering this one.

7. onDestroy(): Called when your finishing activity.

The activity first time loads the methods are called as bilow:
onCreate()
onStart()
onResume()

When try to click back button or finish the activity the following methods are called:
onPause()
onStop()
onDestroy()

When you click on home button the activity goes to background and below methods are called:
onPause()
onStop()

After hitting home button again if your opening the previous activity which is in background the following methods are called:
onRestart()
onStart()
onRestart()


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>
MainActicity.java
package com.blogspot.kantapp.activitylifecycle;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(getApplicationContext(), "onCreate()", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
        Toast.makeText(getApplicationContext(), "onStart()", Toast.LENGTH_SHORT).show();
        super.onStart();
    }

    @Override
    protected void onResume() {
// TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "onResume()", Toast.LENGTH_SHORT).show();

        super.onResume();
    }

    @Override
    protected void onPause() {
// TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "onPause()", Toast.LENGTH_SHORT).show();

        super.onPause();
    }

    @Override
    protected void onStop() {
// TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "onStop()", Toast.LENGTH_SHORT).show();

        super.onStop();
    }

    @Override
    protected void onDestroy() {
// TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "onDestroy()", Toast.LENGTH_SHORT).show();

        super.onDestroy();
    }

    @Override
    protected void onRestart() {
// TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "onRestart()", Toast.LENGTH_SHORT).show();

        super.onRestart();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "onSaveInstanceState()", Toast.LENGTH_SHORT).show();

        super.onSaveInstanceState(outState);
    }
}

No comments:

Post a Comment