Hi we know that life cycle methods of a activity are onCreate() - onStart() - onResume() - onPause() - onStop() - onDestroy().
but what will be the transition of these life cycle methods when going from Activity A to B, below is the example to explain this :
1. Xml file contains button A and B -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Activity A">
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to B"
android:id="@+id/btnActB">
</Button>
</LinearLayout>
2. Activity A with all life cycle methods :
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityA extends Activity implements OnClickListener {
Button btnActB;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnActB = (Button) findViewById(R.id.btnActB);
btnActB.setOnClickListener(this);
Log.d(TAG, "ActivityA: onCreate()");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "ActivityA: onRestart()");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "ActivityA: onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "ActivityA: onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "ActivityA: onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "ActivityA: onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "ActivityA: onDestroy()");
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
}
}
3. ActivityB with all lifecycle methods :
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class ActivityB extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
Log.d(TAG, "ActivityB: onCreate()");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "ActivityB: onRestart()");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "ActivityB: onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "ActivityB: onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "ActivityB: onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "ActivityB: onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "ActivityB: onDestroy()");
}
}
Now running Application and printing activity A life cycle methods :
ActivityA: onCreate()
ActivityA: onStart()
ActivityA: onResume()
and now click on Button and starting activity B :
ActivityA: onPause()
ActivityB: onCreate()
ActivityB: onStart()
ActivityB: onResume()
ActivityA: onStop()
now pressing back button then :
ActivityB: onPause()
ActivityA: onRestart()
ActivityA: onStart()
ActivityA: onResume()
ActivityB: onStop()
ActivityB: onDestroy()
and now pressing back button from Activity A then :
ActivityA: onPause()
ActivityA: onStop()
ActivityA: onDestroy()
so with the following example you can understand the life cycle calls.
0 Comment(s)