学习安卓,不可避免的要了解activity的生命周期。我们通过一些资料知道,activity有六个状态
分别是onCreate、onStart、onResume、onPause、onStop和onDestory。那么它们的调用时间以及顺序是怎么样的呢?学习他们,我们就能跟好的理解activity的工作过程,以便控制逻辑代码的书写。
为了便于观看activity的生命周期,我写了三个activity:
package com.example.test3; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; public class AActivity extends Activity { private TextView textView_lifecycle; private TextView Activity_status=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_a); textView_lifecycle=(TextView)this.findViewById(R.id.textView_lifecycle); Activity_status=(TextView)findViewById(R.id.textView_Activity_status); textView_lifecycle.append(" Activity A.onCreate\n"); Activity_status.setText("Activity A :create"); System.out.println("Activity A :create"); } public void onclick(View v){ Button btn=(Button)findViewById(v.getId()); String btnText=btn.getText().toString(); if(btnText.equals("Start B")){ //创建intent对象,实现页面跳转 Intent intents=new Intent(); intents.setClass(AActivity.this, BActivity.class); AActivity.this.startActivity(intents); } else if(btnText.equals("Start C")){ Intent intents=new Intent(); intents.setClass(AActivity.this, CActivity.class); AActivity.this.startActivity(intents); } else if(btnText.equals("Finish A")){ // android.os.Process.killProcess(android.os.Process.myPid()); finish(); } else{ new AlertDialog.Builder( AActivity.this).setTitle("登录失败!").setMessage("zhanghao:"). setPositiveButton("确定", null).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.a, menu); return true; } @Override protected void onStart() { super.onStart(); textView_lifecycle.append(" Activity A.onStart\n"); Activity_status.setText("Activity A :Start"); System.out.println("Activity A :Start"); } //当按HOME键时,然后再次启动应用时,我们要恢复先前状态 @Override protected void onRestart() { super.onRestart(); textView_lifecycle.append(" Activity A.onReStart\n"); Activity_status.setText("Activity A :ReStart"); System.out.println("Activity A :ReStart"); } @Override protected void onResume() { super.onResume(); textView_lifecycle.append(" Activity A.onResume\n"); Activity_status.setText("Activity A :Resume"); System.out.println("Activity A :Resume"); } @Override protected void onPause() { super.onPause(); textView_lifecycle.append(" Activity A.onPause\n"); Activity_status.setText("Activity A :Pause"); System.out.println("Activity A :Pause"); } @Override protected void onStop() { super.onStop(); textView_lifecycle.append(" Activity A.onStop\n"); Activity_status.setText("Activity A :Stop"); System.out.println("Activity A :Stop"); } @Override protected void onDestroy() { super.onDestroy(); textView_lifecycle.append(" Activity A.onDestroy\n"); Activity_status.setText("Activity A :Destroy"); System.out.println("Activity A :Destroy"); } }
package com.example.test3; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; public class BActivity extends Activity { private TextView textView_lifecycle; private TextView Activity_status=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); textView_lifecycle=(TextView)this.findViewById(R.id.textView_lifecycle); Activity_status=(TextView)findViewById(R.id.textView_Activity_status); textView_lifecycle.append(" Activity B.onCreate\n"); Activity_status.setText("Activity B :create"); System.out.println("Activity B :create"); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.b, menu); return true; } public void onclick(View v){ Button btn=(Button)findViewById(v.getId()); String btnText=btn.getText().toString(); if(btnText.equals("Start A")){ //创建intent对象,实现页面跳转 Intent intents=new Intent(); intents.setClass(BActivity.this, AActivity.class); BActivity.this.startActivity(intents); } else if(btnText.equals("Start C")){ Intent intents=new Intent(); intents.setClass(BActivity.this, CActivity.class); BActivity.this.startActivity(intents); } else if(btnText.equals("Finish B")){ // android.os.Process.killProcess(android.os.Process.myPid()); finish(); } else{ new AlertDialog.Builder( BActivity.this).setTitle("登录失败!").setMessage("zhanghao:"). setPositiveButton("确定", null).show(); } } @Override protected void onStart() { super.onStart(); System.out.println("Activity B :Start"); textView_lifecycle.append(" Activity B.onStart\n"); Activity_status.setText("Activity B :Start"); } //当按HOME键时,然后再次启动应用时,我们要恢复先前状态 @Override protected void onRestart() { super.onRestart(); textView_lifecycle.append(" Activity B.onReStart\n"); Activity_status.setText("Activity B :ReStart"); System.out.println("Activity B :ReStart"); } @Override protected void onResume() { super.onResume(); textView_lifecycle.append(" Activity B.onResume\n"); Activity_status.setText("Activity B :Resume"); System.out.println("Activity B :Resume"); } @Override protected void onPause() { super.onPause(); textView_lifecycle.append(" Activity B.onPause\n"); Activity_status.setText("Activity B :Pause"); System.out.println("Activity B :Pause"); } @Override protected void onStop() { super.onStop(); textView_lifecycle.append(" Activity B.onStop\n"); Activity_status.setText("Activity B :Stop"); System.out.println("Activity B :Stop"); } @Override protected void onDestroy() { super.onDestroy(); textView_lifecycle.append(" Activity B.onDestroy\n"); Activity_status.setText("Activity B :Destroy"); System.out.println("Activity B :Destroy"); } }
package com.example.test3; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.UserHandle; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.IntentSender; import android.content.ServiceConnection; import android.content.SharedPreferences; import android.content.IntentSender.SendIntentException; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.AssetManager; import android.content.res.Configuration; import android.content.res.Resources; import android.content.res.Resources.Theme; import android.database.DatabaseErrorHandler; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.view.Display; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; public class CActivity extends Activity { private TextView textView_lifecycle; private TextView Activity_status=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_c); textView_lifecycle=(TextView)this.findViewById(R.id.textView_lifecycle); Activity_status=(TextView)findViewById(R.id.textView_Activity_status); textView_lifecycle.append(" Activity C.onCreate\n"); Activity_status.setText("Activity C :create"); System.out.println("Activity C :create"); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.c, menu); return true; } public void onclick(View v){ Button btn=(Button)findViewById(v.getId()); String btnText=btn.getText().toString(); if(btnText.equals("Start A")){ //创建intent对象,实现页面跳转 Intent intents=new Intent(); intents.setClass(CActivity.this, AActivity.class); CActivity.this.startActivity(intents); } else if(btnText.equals("Start B")){ Intent intents=new Intent(); intents.setClass(CActivity.this, BActivity.class); CActivity.this.startActivity(intents); } else if(btnText.equals("Finish C")){ // android.os.Process.killProcess(android.os.Process.myPid()); finish(); } else{ new AlertDialog.Builder( CActivity.this).setTitle("Simple Dialog"). setPositiveButton("close", null).show(); } } @Override protected void onStart() { super.onStart(); textView_lifecycle.append(" Activity C.onStart\n"); Activity_status.setText("Activity C :Start"); System.out.println("Activity C.onStart"); } @Override protected void onRestart() { super.onRestart(); textView_lifecycle.append(" Activity C.onReStart\n"); Activity_status.setText("Activity C :ReStart"); System.out.println("Activity C :ReStart"); } @Override protected void onResume() { super.onResume(); textView_lifecycle.append(" Activity C.onResume\n"); Activity_status.setText("Activity C :Resume"); System.out.println("Activity C :Resume"); } @Override protected void onPause() { super.onPause(); textView_lifecycle.append(" Activity C.onPause\n"); Activity_status.setText("Activity C :Pause"); System.out.println("Activity C :Pause"); } @Override protected void onStop() { super.onStop(); textView_lifecycle.append(" Activity C.onStop\n"); Activity_status.setText("Activity C :Stop"); System.out.println("Activity C :Stop"); } @Override protected void onDestroy() { super.onDestroy(); textView_lifecycle.append(" Activity C.onDestroy\n"); Activity_status.setText("Activity C :Destroy"); System.out.println("Activity C :Destroy"); } }
对应的xml文件如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#1E90FF" android:orientation="vertical" android:paddingTop="3dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/title_activity_a_text" android:textSize="30sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/Start_B_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="onclick" android:text="@string/Start_B_btn" /> <Button android:id="@+id/Start_C_btn" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:onClick="onclick" android:text="@string/Start_C_btn" /> <Button android:id="@+id/finish_a_btn" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:onClick="onclick" android:text="@string/finish_a_btn" /> <Button android:id="@+id/Dialog_btn" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:onClick="onclick" android:text="@string/Dialog_btn" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="@string/life_cycle_text" android:textSize="20sp" /> <TextView android:id="@+id/textView_lifecycle" android:layout_width="300dp" android:layout_height="150dp" android:layout_gravity="center_horizontal" android:background="#ffffff" android:text="" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="@string/Activity_status_text" android:textSize="20sp" /> <TextView android:id="@+id/textView_Activity_status" android:layout_width="300dp" android:layout_height="30dp" android:layout_gravity="center_horizontal" android:background="#ffffff" android:text="" /> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#6B8E23" android:orientation="vertical" android:paddingTop="3dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/title_activity_b_text" android:textSize="30sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="@string/Start_A_btn" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="@string/Start_C_btn" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="@string/finish_b_btn" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="@string/Dialog_btn" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="@string/life_cycle_text" android:textSize="20sp" /> <TextView android:id="@+id/textView_lifecycle" android:layout_width="300dp" android:layout_height="150dp" android:layout_gravity="center_horizontal" android:background="#ffffff" android:text="" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="@string/Activity_status_text" android:textSize="20sp" /> <TextView android:id="@+id/textView_Activity_status" android:layout_width="300dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:background="#ffffff" android:text="" /> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FF0000" android:orientation="vertical" android:paddingTop="3dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/title_activity_c_text" android:textSize="30sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="@string/Start_A_btn" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="@string/Start_B_btn" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="@string/finish_c_btn" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="@string/Dialog_btn" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="@string/life_cycle_text" android:textSize="20sp" /> <TextView android:id="@+id/textView_lifecycle" android:layout_width="300dp" android:layout_height="150dp" android:layout_gravity="center_horizontal" android:background="#ffffff" android:text="" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="@string/Activity_status_text" android:textSize="20sp" /> <TextView android:id="@+id/textView_Activity_status" android:layout_width="300dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:background="#ffffff" android:text="" /> </LinearLayout>
运行程序:
点击Start B
点击Start C
,后点回退键
总结各个状态的表现及特点
onCreate 视图创建但不可见,存在于内存中;
onStart 视图可见,但不可交互;
onResume 可交互,最常见的一种状态,app正常使用时的状态
onPause 视图可见,不可交互
onStop 视图不可见,但存在于内存中,占据内存空间
onDestory activity销毁时调用,销毁后生命周期结束