之前对于Android中Activity的生命周期很了解,那张经典的图都能写出来,但是今天发现自己只是单纯的记住了这几个生命周期,而没有真正的理解,这可能就是源于没有动手实验的原因吧。那还是先贴上一张Activity生命周期图:
这个应该是很熟悉的一张图了,但是对于Activity的生命周期从一个ActivityA跳到另一个ActivityB,在然后再ActivityA中调用finish(),两个Activity的生命周期会是:FirstActivity-onCreate()->FirstActivity-onStart()->FirstActivity-onResume()->FirstActivity-onPause()->FirstActivity-onStop()->FirstActivity-onDestory()->SecondActivity-onCreate()->SecondActivity-onStart()->SecondActivity-onResume()的调用顺序,是先执行完一个Activity然后再去执行另一个Activity,但是实验后发现自己弄错了。
public class FirstActivity extends Activity { private static final String TAG="FirstActivity"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); System.out.println("FirstActivity-----onCreate"); Button button=(Button)findViewById(R.id.first); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(FirstActivity.this, SecondActivity.class); startActivity(intent); FirstActivity.this.finish(); } }); } @Override protected void onDestroy() { super.onDestroy(); System.out.println("FirstActivity-----onDestroy"); } @Override protected void onPause() { super.onPause(); System.out.println("FirstActivity-----onPause"); } @Override protected void onRestart() { super.onRestart(); System.out.println("FirstActivity-----onRestart"); } @Override protected void onResume() { super.onResume(); System.out.println("FirstActivity-----onResume"); } @Override protected void onStart() { super.onStart(); System.out.println("FirstActivity-----onStart"); } @Override protected void onStop() { super.onStop(); System.out.println("FirstActivity-----onStop"); } }
public class SecondActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); System.out.println("SecondActivity---------onCreate"); Button button=(Button)findViewById(R.id.second); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(SecondActivity.this, FirstActivity.class); startActivity(intent); SecondActivity.this.finish(); } }); } @Override protected void onDestroy() { super.onDestroy(); System.out.println("SecondActivity---------onDestroy"); } @Override protected void onPause() { super.onPause(); System.out.println("SecondActivity---------onPause"); } @Override protected void onRestart() { super.onRestart(); System.out.println("SecondActivity---------onRestart"); } @Override protected void onResume() { super.onResume(); System.out.println("SecondActivity---------onResume"); } @Override protected void onStart() { super.onStart(); System.out.println("SecondActivity---------onStart"); } @Override protected void onStop() { super.onStop(); System.out.println("SecondActivity---------onStop"); } }
执行的顺序是:
02-09 08:22:09.116: INFO/System.out(29159): FirstActivity-----onCreate 02-09 08:22:09.126: INFO/System.out(29159): FirstActivity-----onStart 02-09 08:22:09.136: INFO/System.out(29159): FirstActivity-----onResume 02-09 08:22:26.066: INFO/System.out(29159): FirstActivity-----onPause 02-09 08:22:26.276: INFO/System.out(29159): SecondActivity---------onCreate 02-09 08:22:26.346: INFO/System.out(29159): SecondActivity---------onStart 02-09 08:22:26.366: INFO/System.out(29159): SecondActivity---------onResume 02-09 08:22:27.466: INFO/System.out(29159): FirstActivity-----onStop 02-09 08:22:27.466: INFO/System.out(29159): FirstActivity-----onDestroy 02-09 08:22:57.667: INFO/System.out(29159): SecondActivity---------onPause 02-09 08:22:58.006: INFO/System.out(29159): FirstActivity-----onCreate 02-09 08:22:58.037: INFO/System.out(29159): FirstActivity-----onStart 02-09 08:22:58.037: INFO/System.out(29159): FirstActivity-----onResume 02-09 08:22:58.757: INFO/System.out(29159): SecondActivity---------onStop 02-09 08:22:58.769: INFO/System.out(29159): SecondActivity---------onDestroy
运行结构可以发现在FirstActivity-onPause()之后便去执行SecondActivity-onCreate()->SecondActivity-onStart()->SecondActivity-onResume()之后才去执行FirstActivity-onStop()->FirstActivity-onDestory()
这种情况是我每次调用新的Activity时都调用了finish()方法,那我不调用finish() 呢?,按理说应该是:
02-09 09:05:44.058: INFO/System.out(6116): FirstActivity-----onCreate 02-09 09:05:44.078: INFO/System.out(6116): FirstActivity-----onStart 02-09 09:05:44.147: INFO/System.out(6116): FirstActivity-----onResume 02-09 09:06:05.147: INFO/System.out(6116): FirstActivity-----onPause 02-09 09:06:05.736: INFO/System.out(6116): SecondActivity---------onCreate 02-09 09:06:05.756: INFO/System.out(6116): SecondActivity---------onStart 02-09 09:06:05.786: INFO/System.out(6116): SecondActivity---------onResume 02-09 09:06:05.867: INFO/System.out(6116): FirstActivity-----onStop 02-09 09:06:10.817: INFO/System.out(6116): SecondActivity---------onPause 02-09 09:06:10.947: INFO/System.out(6116): FirstActivity-----onRestart 02-09 09:06:10.957: INFO/System.out(6116): FirstActivity-----onStart 02-09 09:06:10.967: INFO/System.out(6116): FirstActivity-----onResume 02-09 09:06:11.477: INFO/System.out(6116): SecondActivity---------onStop
可是运行的结果却是:
02-09 09:10:16.866: INFO/System.out(7113): FirstActivity-----onCreate 02-09 09:10:16.886: INFO/System.out(7113): FirstActivity-----onStart 02-09 09:10:16.886: INFO/System.out(7113): FirstActivity-----onResume 02-09 09:10:19.606: INFO/System.out(7113): FirstActivity-----onPause 02-09 09:10:20.007: INFO/System.out(7113): SecondActivity---------onCreate 02-09 09:10:20.017: INFO/System.out(7113): SecondActivity---------onStart 02-09 09:10:20.038: INFO/System.out(7113): SecondActivity---------onResume 02-09 09:10:20.826: INFO/System.out(7113): FirstActivity-----onStop 02-09 09:10:22.657: INFO/System.out(7113): SecondActivity---------onPause 02-09 09:10:22.826: INFO/System.out(7113): FirstActivity-----onCreate 02-09 09:10:22.826: INFO/System.out(7113): FirstActivity-----onStart 02-09 09:10:22.836: INFO/System.out(7113): FirstActivity-----onResume 02-09 09:10:23.426: INFO/System.out(7113): SecondActivity---------onStop
在网上查了一下发现是因为没有加android:launchMode="singleInstance"的原因。
那现在就有一个问题了,上面出现这种情况是因为我没有加android:launchMode="singleInstance"的原因,那我调用了finish()的情况下,加了android:launchMode="singleInstance"和没有加也有这种区别么?经过实验是没有的:
02-09 09:12:11.077: INFO/System.out(7526): FirstActivity-----onCreate 02-09 09:12:11.087: INFO/System.out(7526): FirstActivity-----onStart 02-09 09:12:11.107: INFO/System.out(7526): FirstActivity-----onResume 02-09 09:12:16.207: INFO/System.out(7526): FirstActivity-----onPause 02-09 09:12:16.567: INFO/System.out(7526): SecondActivity---------onCreate 02-09 09:12:16.618: INFO/System.out(7526): SecondActivity---------onStart 02-09 09:12:16.637: INFO/System.out(7526): SecondActivity---------onResume 02-09 09:12:16.767: INFO/System.out(7526): FirstActivity-----onStop 02-09 09:12:16.777: INFO/System.out(7526): FirstActivity-----onDestroy 02-09 09:12:27.156: INFO/System.out(7526): SecondActivity---------onPause 02-09 09:12:27.616: INFO/System.out(7526): FirstActivity-----onCreate 02-09 09:12:27.630: INFO/System.out(7526): FirstActivity-----onStart 02-09 09:12:27.637: INFO/System.out(7526): FirstActivity-----onResume 02-09 09:12:27.805: INFO/System.out(7526): SecondActivity---------onStop 02-09 09:12:27.819: INFO/System.out(7526): SecondActivity---------onDestroy