lifecycle of Android activity

It's a exercise to understand the lifecycle of a Android activity: onCreate(), onStart(), onRestart(), onResume(), onPause(), onStop(), onDestroy(). Through the exercise, you can note the difference among exit a activity by HOME key, BACK key and finish() call.



main.xml
<?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="@string/hello"
/>
<TextView
android:id="@+id/textlife"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24px"
android:text="App first started"
/>
<Button
android:id="@+id/buttonafterstart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Press It!"
/>
<Button
android:id="@+id/buttonexit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EXIT"
/>
</LinearLayout>


AndroidLifeCycle.java
package com.exercise.AndroidLifeCycle;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidLifeCycle extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Toast.makeText(this, "onCreate()", Toast.LENGTH_LONG).show();

final TextView tvTextLife = (TextView)findViewById(R.id.textlife);
Button btAfterStart = (Button)findViewById(R.id.buttonafterstart);
Button btExit = (Button)findViewById(R.id.buttonexit);

btAfterStart.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tvTextLife.setText("App started before!");
}});

btExit.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}});
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "onDestroy()", Toast.LENGTH_LONG).show();
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Toast.makeText(this, "onPause()", Toast.LENGTH_LONG).show();
}

@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Toast.makeText(this, "onRestart()", Toast.LENGTH_LONG).show();
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "onResume()", Toast.LENGTH_LONG).show();
}

@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Toast.makeText(this, "onStart()", Toast.LENGTH_LONG).show();
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Toast.makeText(this, "onStop()", Toast.LENGTH_LONG).show();
}


}


Download the files.