Saturday, May 25, 2013

Lesson 8. Android Life Cycle

Android Life Cycle


Android Activity Lifecycle Example Code Description
Here we will learn complete details about an Android Activity Lifecycle Example with details Code Description. Here we will learn details about all real time scenarios and different states of an Android Activity. Once you download the sample code example from here, then you can find line by line code description for an Android Activity. If you are new to Android Activity, then you might like to read my article Android Fundamentals and Components. Anyway lets have a quick reminder on Android Activity Lifecycle. Below image will give us a overall idea about an Activity’s complete LifeCycle, then we will implement all details in our example.




Android Activity states

onCreate()

onCreat Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.
Always followed by onStart().

onRestart()

onRestart Called after your activity has been stopped, prior to it being started again.
Always followed by onStart()

onStart()

Called when the activity is becoming visible to the user.
Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

onResume()

Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.

Always followed by onPause().

onPause()

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 other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.
Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.

onStop()

onStop Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.

Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.

onDestroy()

The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with theisFinishing() method.

ActivityLifeCycle Example code :-


package com.learnsimply.activitylifecycle;

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

public class ActivityLifeCycle extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(this,"onCreate", Toast.LENGTH_SHORT).show();
}

public void onStart() {
super.onStart();
Toast.makeText(this,"onStart.", Toast.LENGTH_SHORT).show();
}

public void onRestart() {
super.onRestart();
Toast.makeText(this,"onRestart.", Toast.LENGTH_SHORT).show();
}

public void onResume() {
super.onResume();
Toast.makeText(this,"onResume.", Toast.LENGTH_SHORT).show();
}

public void onPause() {
super.onPause();
Toast.makeText(this,"onPause.", Toast.LENGTH_SHORT).show();
}

public void onStop() {
super.onStop();
Toast.makeText(this,"onStop.", Toast.LENGTH_SHORT).show();
}

public void onDestroy() {
super.onStop();
Toast.makeText(this,"onDestroy.", Toast.LENGTH_SHORT).show();
}

}