Android活动生命周期介绍
示例
假定具有MainActivity的应用程序可以通过单击按钮来调用NextActivity。
public class MainActivity extends AppCompatActivity { private final String LOG_TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(LOG_TAG, "calling onCreate from MainActivity"); } @Override protected void onStart() { super.onStart(); Log.d(LOG_TAG, "calling onStart from MainActivity"); } @Override protected void onResume() { super.onResume(); Log.d(LOG_TAG, "calling onResume from MainActivity"); } @Override protected void onPause() { super.onPause(); Log.d(LOG_TAG, "calling onPause from MainActivity"); } @Override protected void onStop() { super.onStop(); Log.d(LOG_TAG, "calling onStop from MainActivity"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(LOG_TAG, "calling onDestroy from MainActivity"); } @Override protected void onRestart() { super.onRestart(); Log.d(LOG_TAG, "calling onRestart from MainActivity"); } public void toNextActivity(){ Log.d(LOG_TAG, "calling Next Activity"); Intent intent = new Intent(this, NextActivity.class); startActivity(intent); } }
和
public class NextActivity extends AppCompatActivity { private final String LOG_TAG = NextActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_next); Log.d(LOG_TAG, "calling onCreate from Next Activity"); } @Override protected void onStart() { super.onStart(); Log.d(LOG_TAG, "calling onStart from Next Activity"); } @Override protected void onResume() { super.onResume(); Log.d(LOG_TAG, "calling onResume from Next Activity"); } @Override protected void onPause() { super.onPause(); Log.d(LOG_TAG, "calling onPause from Next Activity"); } @Override protected void onStop() { super.onStop(); Log.d(LOG_TAG, "calling onStop from Next Activity"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(LOG_TAG, "calling onDestroy from Next Activity"); } @Override protected void onRestart() { super.onRestart(); Log.d(LOG_TAG, "calling onRestart from Next Activity"); } }
在首次创建应用程序
d/MainActivity:从主叫MainActivity的onCreate
d/MainActivity:从主叫MainActivity调用onStart
d/MainActivity:从主叫MainActivity的onResume
被称为
屏幕休眠时
08:11:03.142D/MainActivity:从MainActivity调用onPause
08:11:03.192D/MainActivity:从MainActivity调用onStop
。并再次当它醒来
08:11:55.922d/MainActivity:从MainActivity调用onRestart
08:11:55.962d/MainActivity:从MainActivity调用在onStart
11:55.962d/MainActivity:08从MainActivity调用的onResume
被称为
情况1:当从主活动
D/MainActivity调用下一个活动:调用下一个活动
D/MainActivity:从主活动D/
NextActivity调用onPause:从下一个活动
D/NextActivity调用onCreate:从下一个活动D/NextActivity调用onStart
:从下一活动
D/MainActivity:从MainActivity调用onStop
当使用返回按钮
D/NextActivity从下一个活动返回主活动时:从下一个活动
D/MainActivity调用onPause:从MainActivity
D/MainActivity调用onRestart:从MainActivityD/MainActivity调用onStart
:从MainActivity
D/NextActivity调用onResume:从下一个活动
D/NextActivity调用onStop:从下一个活动调用onDestroy
情况2:当“活动”被部分遮盖(按“概览”按钮时)或当应用程序进入后台并且另一个应用程序完全遮盖它时
D/MainActivity:从MainActivity调用onPause
D/MainActivity:从MainActivity调用onStop
且应用返回前景准备接受用户输入,
d/MainActivity:从主叫MainActivityonRestart
d/MainActivity:从主叫MainActivity调用onStart
d/MainActivity:从主叫MainActivity的onResume
被称为
情况3:当一个活动被调用以实现隐式意图并且用户做出选择时。例如,当按下共享按钮时,用户必须从显示的应用程序列表中选择一个应用程序
D/MainActivity:从MainActivity调用onPause
活动可见,但现在不活动。当选择完成和应用处于活动状态
d/MainActivity:从MainActivity调用的onResume
被称为
案例4:
当应用程序在中被杀死时background(tofreeresourcesforanotherforegroundapp),onPause(用于蜂窝前设备)或onStop(用于蜂窝设备)将是终止该应用程序之前的最后一次调用。
每次运行应用程序时,将最多一次调用onCreate和onDestroy。但是在生命周期中可能会多次调用onPause,onStop,onRestart,onStart,onResume。