2.4.1 返回栈
每启动一个新的流动,会在返回栈中入栈,并处于栈顶的地位。每销毁一个流动,处于栈顶的流动会出栈,前一个入栈的流动会从新处于栈顶的地位。
2.4.2 活动状态
- 运行状态:位于返回栈栈顶的流动。
- 暂停状态:不在栈顶地位,但依然可见。
- 进行状态:不在栈顶地位,齐全不可见。可能会被零碎回收。
- 销毁状态:在返回栈中移除的流动。
2.4.3 流动的生存期
- onCreate():流动第一次被创立时调用,在此办法中实现流动的初始化工作。
- onStart():流动由不可见变为可见时调用。
- onResume():在流动筹备好和用户进行交互时调用。此时流动位于栈顶,并处于运行状态。
- onPause():零碎筹备去启动或复原另一个流动时调用。启动新的对话框流动,此办法会被调用。
- onStop():在流动齐全不可见时调用。启动新的对话框流动,此办法不会被调用。
- onDestroy():流动被销毁前被调用。
- onRestart():流动由进行状态变为运行状态前调用。
能够将流动分为3中生存期:
- 残缺生存期。在 onCreate() 和 onDestroy() 之间所经验的。
- 可见生存期。在 onStart() 和 onStop() 之间所经验的。
- 前台生存期。在 onResume() 和 onPause() 之间所经验的。
2.4.4 体验流动的生命周期
# app/src/main/res/layout/normal_layout.xml<?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"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is a normal activity" /></LinearLayout>
# app/src/main/res/layout/dialog_layout.xml<?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"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is a dialog activity" /></LinearLayout>
# app/src/main/AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.activitylifecycletest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NormalActivity"> </activity> <activity android:name=".DialogActivity" # DialogActivity 流动应用 Dialog 对话框主题 android:theme="@android:style/Theme.Dialog"> </activity> </application></manifest>
# app/src/main/res/layout/activity_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="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/start_normal_activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start NormalActivity" /> <Button android:id="@+id/start_dialog_activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start DialogActivity" /></LinearLayout>
# app/src/main/java/com/example/activitylifecycletest/MainActivity.javapublic class MainActivity extends AppCompatActivity { public static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate"); setContentView(R.layout.activity_main); Button startNormalActivity = (Button) findViewById(R.id.start_normal_activity); Button startDialogActivity = (Button) findViewById(R.id.start_dialog_activity); // 启动 NormalActivity 流动 startNormalActivity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, NormalActivity.class); startActivity(intent); } }); // 启动 DialogActivity 流动 startDialogActivity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, DialogActivity.class); startActivity(intent); } }); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart"); }}
程序启动:Create - Start - Resume
按钮一:Pause - Stop
返回按钮:Restart - Start - Resume
退出程序:Pause - Stop - Destroy
程序启动:Create - Start - Resume
按钮二:Pause
返回按钮:Resume
退出程序:Pause - Stop - Destroy
2.4.5 流动被回收了怎么办
流动进入进行状态,可能会被零碎回收。用户返回流动后,流动不会执行 onRestart() 办法,而是会执行 onCreate() 办法,从新创立一次该流动。
Android 提供了 onSaveInstanceState() 回调办法,保障在流动被回收前被调用,可用于保留上一个流动的长期数据:
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // 保留长期数据 String tempData = "Something you just typed"; outState.putString("data_key", tempData); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate"); setContentView(R.layout.activity_main); // 取出长期数据 if(savedInstanceState != null) { String tempData = savedInstanceState.getString("data_key"); Log.d(TAG, tempData); } }