标签:tle 执行 use ima 生命周期 resume out public .com
下图是Android开发官网的原图,清晰表达了Activity生命周期中每个方法所处位置及调用流程。
上图对Activity的执行流程表达很到位,但是对每个方法在执行时,我们的设备处于一种什么状态,其中注释表达的不是很细致,有待进一步深入研究。
下表是Android官网对Activity生命周期中各个方法的详解。
注:通过上面表格中的信息描述我们不难发现一下几点:
有两个Activity,MainActivity和TwoActivity,其代码如下,后面将执行几个常见的操作,看看其生命周期流程究竟如何运行。
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn_onClick).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, TwoActivity.class)); } }); Log.i("info", "MainActivity --> onCreate()"); } @Override protected void onStart() { super.onStart(); Log.i("info", "MainActivity --> onStart()"); } @Override protected void onResume() { super.onResume(); Log.i("info", "MainActivity --> onResume()"); } @Override protected void onRestart() { super.onRestart(); Log.i("info", "MainActivity --> onRestart()"); } @Override protected void onPause() { super.onPause(); Log.i("info", "MainActivity --> onPause()"); } @Override protected void onStop() { super.onStop(); Log.i("info", "MainActivity --> onStop()"); } @Override protected void onDestroy() { super.onDestroy(); Log.i("info", "MainActivity --> onDestroy()"); } }
public class TwoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two_layout); Log.i("info", "TwoActivity --> onCreate()"); } @Override protected void onStart() { super.onStart(); Log.i("info", "TwoActivity --> onStart()"); } @Override protected void onRestart() { super.onRestart(); Log.i("info", "TwoActivity --> onRestart()"); } @Override protected void onResume() { super.onResume(); Log.i("info", "TwoActivity --> onResume()"); } @Override protected void onPause() { super.onPause(); Log.i("info", "TwoActivity --> onPause()"); } @Override protected void onStop() { super.onStop(); Log.i("info", "TwoActivity --> onStop()"); } @Override protected void onDestroy() { super.onDestroy(); Log.i("info", "TwoActivity --> onDestroy()"); } }
结果预测:
MainActivity --> onCreate()
MainActivity --> onStart()
MainActivity --> onResume()
真实结果:
果然一致。
结果预测:
MainActivity –> onPause()
TwoActivity –> onCreate()
TwoActivity –> onStart()
TwoActivity –> onResume()
MainActivity –> onStop()
真实结果:
和预测顺序一致。
结果预测:
TwoAcitvity –> onPause()
MainActivity –> onRestart()
MainActivity –> onStrat()
MainActivity –> onResume()
TwoActivity –> onStop()
TwoActivity –> onDestroy()
真实结果:
顺序一致,证明理论是正确的。
结果预测:
MainActivity –> onPause()
MainActivity –> onStop()
MainActivity –> onDestroy()
真实结果:
标签:tle 执行 use ima 生命周期 resume out public .com
原文地址:http://www.cnblogs.com/shidian/p/7966884.html