Activity生命周期(二)
1. 什么是生命周期
2. Activity的生命周期函数
3. 生命周期的调用时机
1. 什么是生命周期
类似人的一生
2. Activity的生命周期函数
生命周期函数不是我们来调用的,而是操作系统来管理的
我们能做的就是通过复写函数 来完成不同的功能
上帝赐予我们每个人童年 青年 中年 老年
但每二个时间段我们怎么活是我们自己的选择
3. 生命周期的调用时机
采用两个Activity跳转过程的打印内容看出上述几个生命周期函数调用时机
新建class 叫 OtherActivity, 在OtherActivity中复写上述几个生命周期函数
复写OtherActivity.java如下
1 package first.pack; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.util.Log; 6 7 public class OtherActivity extends Activity { 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 Log.i("tag","OtherActivity:OnCreate"); 13 } 14 15 @Override 16 protected void onStart() { 17 super.onStart(); 18 Log.i("tag","OtherActivity:OnStart"); 19 20 } 21 22 @Override 23 protected void onRestart() { 24 super.onRestart(); 25 Log.i("tag","OtherActivity:OnRestart"); 26 27 } 28 29 @Override 30 protected void onResume() { 31 Log.i("tag","OtherActivity:OnResume"); 32 super.onResume(); 33 } 34 35 @Override 36 protected void onPause() { 37 Log.i("tag","OtherActivity:OnPause"); 38 super.onPause(); 39 } 40 41 @Override 42 protected void onStop() { 43 Log.i("tag","OtherActivity:OnStop"); 44 super.onStop(); 45 } 46 47 @Override 48 protected void onDestroy() { 49 Log.i("tag","OtherActivity:OnDestroy"); 50 super.onDestroy(); 51 } 52 }
类似的,复写MainActivity.java如下
1 package first.pack; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.support.v7.app.ActionBar; 5 import android.support.v4.app.Fragment; 6 import android.os.Bundle; 7 import android.util.Log; 8 import android.view.LayoutInflater; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.View; 12 import android.view.ViewGroup; 13 import android.os.Build; 14 15 public class MainActivity extends ActionBarActivity { 16 17 @Override 18 public void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 if (savedInstanceState == null) { 23 getSupportFragmentManager().beginTransaction() 24 .add(R.id.container, new PlaceholderFragment()) 25 .commit(); 26 } 27 } 28 29 30 @Override 31 public boolean onCreateOptionsMenu(Menu menu) { 32 33 // Inflate the menu; this adds items to the action bar if it is present. 34 getMenuInflater().inflate(R.menu.main, menu); 35 return true; 36 } 37 38 @Override 39 public boolean onOptionsItemSelected(MenuItem item) { 40 // Handle action bar item clicks here. The action bar will 41 // automatically handle clicks on the Home/Up button, so long 42 // as you specify a parent activity in AndroidManifest.xml. 43 int id = item.getItemId(); 44 if (id == R.id.action_settings) { 45 return true; 46 } 47 return super.onOptionsItemSelected(item); 48 } 49 50 /** 51 * A placeholder fragment containing a simple view. 52 */ 53 public static class PlaceholderFragment extends Fragment { 54 55 public PlaceholderFragment() { 56 } 57 58 @Override 59 public View onCreateView(LayoutInflater inflater, ViewGroup container, 60 Bundle savedInstanceState) { 61 View rootView = inflater.inflate(R.layout.fragment_main, container, false); 62 Log.i("tag","MainActivity:OnCreate"); 63 return rootView; 64 } 65 66 @Override 67 public void onStart() { 68 super.onStart(); 69 Log.i("tag","MainActivity:OnStart"); 70 71 } 72 73 @Override 74 public void onResume() { 75 Log.i("tag","MainActivity:OnResume"); 76 super.onResume(); 77 } 78 79 @Override 80 public void onPause() { 81 Log.i("tag","MainActivity:OnPause"); 82 super.onPause(); 83 } 84 85 @Override 86 public void onStop() { 87 Log.i("tag","MainActivity:OnStop"); 88 super.onStop(); 89 } 90 91 @Override 92 public void onDestroy() { 93 Log.i("tag","MainActivity:OnDestroy"); 94 super.onDestroy(); 95 } 96 } 97 }
接着为OtherActivity新建一个布局文件Other.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <TextView 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:text="Other" 10 /> 11 12 </LinearLayout>
我们让OtherActivity使用Other.xml
1 public class OtherActivity extends Activity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.other); // 使用布局文件 7 Log.i("tag","OtherActivity:OnCreate"); 8 } 9 10 ... 11 12 }
接着我们需要在manifest文件中对其注册
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="first.pack" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="19" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="first.pack.MainActivity" 18 android:label="@string/FirstMirror" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 <activity 26 android:name="first.pack.OtherActivity" //添加 27 android:label="@string/SecondMirror" > //添加 28 </activity> 29 </application> 30 31 </manifest>
OK,下面我们为第一个Activity添加一个按钮
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="first.pack.MainActivity$PlaceholderFragment" > 10 11 <Button 12 android:id="@+id/firstButton" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="OpenDoor" /> 16 17 </RelativeLayout>
为此按钮在MainActivity中添加监听器
1 public static class PlaceholderFragment extends Fragment { 2 3 private Button button; 4 5 public PlaceholderFragment() { 6 } 7 8 @Override 9 public View onCreateView(LayoutInflater inflater, ViewGroup container, 10 Bundle savedInstanceState) { 11 View rootView = inflater.inflate(R.layout.fragment_main, container, false); 12 13 button = (Button)rootView.findViewById(R.id.firstButton); 14 15 ButtonListener buttonListener = new ButtonListener(); 16 button.setOnClickListener(buttonListener); 17 18 Log.i("tag","MainActivity:OnCreate"); 19 return rootView; 20 } 21 22 class ButtonListener implements android.view.View.OnClickListener{ 23 24 @Override 25 public void onClick(View v) { 26 Intent intent = new Intent(); 27 intent.setClass(getActivity(),OtherActivity.class); 28 startActivity(intent); 29 } 30 } 31 32 @Override 33 public void onStart() { 34 super.onStart(); 35 Log.i("tag","MainActivity:OnStart"); 36 37 } 38 39 @Override 40 public void onResume() { 41 Log.i("tag","MainActivity:OnResume"); 42 super.onResume(); 43 } 44 45 @Override 46 public void onPause() { 47 Log.i("tag","MainActivity:OnPause"); 48 super.onPause(); 49 } 50 51 @Override 52 public void onStop() { 53 Log.i("tag","MainActivity:OnStop"); 54 super.onStop(); 55 } 56 57 @Override 58 public void onDestroy() { 59 Log.i("tag","MainActivity:OnDestroy"); 60 super.onDestroy(); 61 } 62 }
接下来就是分析运行结果的过程了,首先启动程序,界面 和 打印 如下所示
点击OpenDoor之后
点击返回键之后
总结: OnCreate 创建新的Activity
OnStart 变得可见时调用
OnResume 可以与用户交互时候调用
OnPause 要启动另外一个Activity时调用
OnStop 变得不可见时调用
OnDestroy 从历史栈中弹出就被销毁