Tuesday, February 19, 2013

What is the life cycle of an Android activity? : Activity Life cycle in Android


Hello Friends,
          Today I am going to share my very Important research on Activity Life Cycle in Android. We all are good in development but sometimes we get  confused or get stucked in Activity Life Cycle, For that I have invested  lot of time and  i tested it manually and finally got some positive result, thats what I want share with you.
Hope it will help you.

You can refer below link also it will give you more detail-
http://developer.android.com/reference/android/app/Activity.html


---------------------------------------------------------------------
First Page:

-------------------------------------------------------------------------------------------
Second Page:

--------------------------------------------------------------------------

Activity Defnition: A single screen in an application, with supporting Java code.
Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity,the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

When the Activity first time loads then following events are called:
onCreate()
onStart()
onResume()

When we move to next Activity then below events called:
onPause()
onStop()

When we back from Next Activity or press back button then below events called:
onRestart()
onStart()
onResume()

When we press Device Home button, application running in background and below events called:
onPause()
onStop()

When we launch, application from background then below events called:
onRestart()
onStart()
onResume()

When we click the back button OR try to finish the activity then below events are called:
onPause()
onStop()
onDestroy()

For more detail please copy below code and check your log step by step it will give you more help-
1)MainActivity.java

package com.example.activitylifecycle;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
Button btnNext;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNext = (Button) findViewById(R.id.button1);
Log.i("Activity Life Cycle Result:", "On Create call");
/**
* Next Button clicked
*/
btnNext.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, NextPage.class);
startActivity(intent);
}
});

}

@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i("Activity Life Cycle Result:", "On start call");
}

@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.i("Activity Life Cycle Result:", "On Re-start call");
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("Activity Life Cycle Result:", "On resume call");
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i("Activity Life Cycle Result:", "On pause call");
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i("Activity Life Cycle Result:", "On stop call");
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("Activity Life Cycle Result:", "On destroy call");
}
}


2)NextPage.java

package com.example.activitylifecycle;

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

public class NextPage extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);

}
}


3)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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:padding="@dimen/padding_medium"
        android:text="Activity Life Cycle Testing"
        android:textSize="25sp"
        tools:context=".MainActivity" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="118dp"
        android:text="Next Page" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:padding="@dimen/padding_medium"
        android:text="Please check your Log"
        android:textSize="25sp"
        tools:context=".MainActivity" />

</RelativeLayout>

4)next.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


Your  any Comments and Suggestions would be greatly appreciated.

Thanks,

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.