In Android, Activity is like state of the application. Each activity has a layout. Activity has a lifecycle.
1. onCreate()
2. onStart()
3. onRestart()
4. onResume()
5. onPause()
6. onStop()
7. onDestroy()
ActivityA.java
package com.example.activitylifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import com.example.activitylifecycle.util.StatusTracker;
import com.example.activitylifecycle.util.Utils;
public class ActivityA extends Activity {
private String mActivityName;
private TextView mStatusView;
private TextView mStatusAllView;
private StatusTracker mStatusTracker = StatusTracker.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
mActivityName = getString(R.string.activity_a);
mStatusView = (TextView)findViewById(R.id.status_view_a);
mStatusAllView = (TextView)findViewById(R.id.status_view_all_a);
mStatusTracker.setStatus(mActivityName, getString(R.string.on_create));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onStart() {
super.onStart();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_start));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onRestart() {
super.onRestart();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_restart));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onResume() {
super.onResume();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_resume));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onPause() {
super.onPause();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_pause));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onStop() {
super.onStop();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_stop));
}
@Override
protected void onDestroy() {
super.onDestroy();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_destroy));
mStatusTracker.clear();
}
public void startDialog(View v) {
Intent intent = new Intent(ActivityA.this, DialogActivity.class);
startActivity(intent);
}
public void startActivityB(View v) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
}
public void startActivityC(View v) {
Intent intent = new Intent(ActivityA.this, ActivityC.class);
startActivity(intent);
}
public void finishActivityA(View v) {
ActivityA.this.finish();
}
}
ActivityB.java
package com.example.activitylifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import com.example.activitylifecycle.util.StatusTracker;
import com.example.activitylifecycle.util.Utils;
public class ActivityB extends Activity {
private String mActivityName;
private TextView mStatusView;
private TextView mStatusAllView;
private StatusTracker mStatusTracker = StatusTracker.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
mActivityName = getString(R.string.activity_b_label);
mStatusView = (TextView)findViewById(R.id.status_view_b);
mStatusAllView = (TextView)findViewById(R.id.status_view_all_b);
mStatusTracker.setStatus(mActivityName, getString(R.string.on_create));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onStart() {
super.onStart();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_start));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onRestart() {
super.onRestart();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_restart));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onResume() {
super.onResume();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_resume));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onPause() {
super.onPause();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_pause));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onStop() {
super.onStop();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_stop));
}
@Override
protected void onDestroy() {
super.onDestroy();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_destroy));
}
public void startDialog(View v) {
Intent intent = new Intent(ActivityB.this, DialogActivity.class);
startActivity(intent);
}
public void startActivityA(View v) {
Intent intent = new Intent(ActivityB.this, ActivityA.class);
startActivity(intent);
}
public void startActivityC(View v) {
Intent intent = new Intent(ActivityB.this, ActivityC.class);
startActivity(intent);
}
public void finishActivityB(View v) {
ActivityB.this.finish();
}
}
ActivityC.java
package com.example.android.lifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import com.example.android.lifecycle.util.StatusTracker;
import com.example.android.lifecycle.util.Utils;
public class ActivityC extends Activity {
private String mActivityName;
private TextView mStatusView;
private TextView mStatusAllView;
private StatusTracker mStatusTracker = StatusTracker.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
mActivityName = getString(R.string.activity_c_label);
mStatusView = (TextView)findViewById(R.id.status_view_c);
mStatusAllView = (TextView)findViewById(R.id.status_view_all_c);
mStatusTracker.setStatus(mActivityName, getString(R.string.on_create));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onStart() {
super.onStart();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_start));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onRestart() {
super.onRestart();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_restart));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onResume() {
super.onResume();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_resume));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onPause() {
super.onPause();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_pause));
Utils.printStatus(mStatusView, mStatusAllView);
}
@Override
protected void onStop() {
super.onStop();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_stop));
}
@Override
protected void onDestroy() {
super.onDestroy();
mStatusTracker.setStatus(mActivityName, getString(R.string.on_destroy));
}
public void startDialog(View v) {
Intent intent = new Intent(ActivityC.this, DialogActivity.class);
startActivity(intent);
}
public void startActivityA(View v) {
Intent intent = new Intent(ActivityC.this, ActivityA.class);
startActivity(intent);
}
public void startActivityB(View v) {
Intent intent = new Intent(ActivityC.this, ActivityB.class);
startActivity(intent);
}
public void finishActivityC(View v) {
ActivityC.this.finish();
}
}
DialogActivity.java
package com.example.activitylifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_dialog);
}
public void finishDialog(View v) {
DialogActivity.this.finish();
}
}
StatusTracker.java
package com.example.activitylifecycle.util;
import java.util.*;
public class StatusTracker {
private Map<String, String> mStatusMap;
private List<String> mMethodList;
private static StatusTracker ourInstance = new StatusTracker();
private static final String STATUS_SUFFIX = "ed";
public static StatusTracker getInstance() {
return ourInstance;
}
private StatusTracker() {
mStatusMap = new LinkedHashMap<String, String>();
mMethodList = new ArrayList<String>();
}
public List<String> getMethodList() {
return mMethodList;
}
public void clear() {
mMethodList.clear();
mStatusMap.clear();
}
public void setStatus(String activityName, String status) {
mMethodList.add(activityName + "." + status + "()");
if (mStatusMap.containsKey(activityName)) mStatusMap.remove(activityName);
mStatusMap.put(activityName, status);
}
public String getStatus(String activityName) {
String status = mStatusMap.get(activityName);
status = status.substring(2, status.length());
// String manipulation to ensure the status value is spelled correctly.
if (status.endsWith("e")) {
status = status.substring(0, status.length() - 1);
}
if (status.endsWith("p")) {
status = status + "p";
}
status = status + STATUS_SUFFIX;
return status;
}
public Set<String> keySet() {
return mStatusMap.keySet();
}
}
Utils.java
package com.example.activitylifecycle.util;
import android.os.Handler;
import android.widget.TextView;
import java.util.List;
public class Utils {
private static StatusTracker mStatusTracker = StatusTracker.getInstance();
public static void printStatus(final TextView viewMethods, final TextView viewStatus) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
StringBuilder sbMethods = new StringBuilder();
List<String> listMethods = mStatusTracker.getMethodList();
for (String method : listMethods) {
sbMethods.insert(0, method + "\r\n");
}
if(viewMethods != null) {
viewMethods.setText(sbMethods.toString());
}
StringBuilder sbStatus = new StringBuilder();
for (String key : mStatusTracker.keySet()) {
sbStatus.insert(0,key + ": " + mStatusTracker.getStatus(key) + "\n");
}
if(viewStatus != null) {
viewStatus.setText(sbStatus.toString());
}
}
}, 750);
}
}
Layout ActivityA
Other layout ActivityB and ActivityC also similar to ActivityA.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark_blue"
android:padding="8dip"
>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/activity_a"
android:gravity="center_horizontal"
android:textSize="@dimen/font_large"
android:textColor="@color/light_blue"
android:paddingBottom="16dip"
/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
<Button
android:id="@+id/btn_start_b"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/btn_start_b_label"
android:onClick="startActivityB"
/>
<Button
android:id="@+id/btn_start_c"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/btn_start_c_label"
android:layout_toRightOf="@id/btn_start_b"
android:onClick="startActivityC"
/>
<Button
android:id="@+id/btn_finish_a"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/btn_finish_a_label"
android:layout_toRightOf="@id/btn_start_c"
android:onClick="finishActivityA"
/>
<Button
android:id="@+id/btn_start_dialog"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/btn_start_dialog_label"
android:layout_toRightOf="@id/btn_finish_a"
android:onClick="startDialog"
/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/font_medium"
android:text="@string/lifecycle_method_list"
android:textColor="@color/light_blue"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="160dp">
<TextView
android:id="@+id/status_view_a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:textColor="@color/black"
android:padding="4dip"
android:typeface="monospace"
/>
</ScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/font_medium"
android:text="@string/activity_status"
android:textColor="@color/light_blue"
/>
<TextView
android:id="@+id/status_view_all_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:textColor="@color/black"
android:padding="4dip"
android:typeface="monospace"
/>
</LinearLayout>
AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitylifecycle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ActivityA"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityB" />
<activity android:name=".ActivityC" />
<activity android:name=".DialogActivity"
android:theme="@android:style/Theme.Dialog">
</activity>
</application>
</manifest>