赞
踩
每次激活Activity时(startActivity),都创建Activity实例,并放入任务栈
AActivity为standard模式
# 启动AActivity
04-21 15:00:40.687 5013-5013/? D/AActivity: onCreate()
04-21 15:00:40.690 5013-5013/? D/AActivity: onStart()
04-21 15:00:40.690 5013-5013/? D/AActivity: onResume()
# 启动AActivity
04-21 15:00:43.038 5013-5013/com.example.myapplication D/AActivity: onPause()
04-21 15:00:43.047 5013-5013/com.example.myapplication D/AActivity: onCreate()
04-21 15:00:43.048 5013-5013/com.example.myapplication D/AActivity: onStart()
04-21 15:00:43.048 5013-5013/com.example.myapplication D/AActivity: onResume()
04-21 15:00:43.359 5013-5013/com.example.myapplication D/AActivity: onStop()
从上面的输出可以看出,前台Activity调用onResume()之后才会调用被覆盖的Activity的onStop()方法
如果某个Activity自己激活自己,即任务栈栈顶就是该Activity,则不需要创建,其余情况都要创建Activity实例。
AActivity为singleTop模式,BActivity为standard模式
# 启动AActivity 04-21 15:06:54.064 5151-5151/? D/AActivity: onCreate() 04-21 15:06:54.064 5151-5151/? D/AActivity: onStart() 04-21 15:06:54.065 5151-5151/? D/AActivity: onResume() # 再次启动AActivity 04-21 15:06:59.454 5151-5151/com.example.myapplication D/AActivity: onPause() 04-21 15:06:59.454 5151-5151/com.example.myapplication D/AActivity: onNewIntent() 04-21 15:06:59.454 5151-5151/com.example.myapplication D/AActivity: onResume() # 启动BActivity 04-21 15:07:09.352 5151-5151/com.example.myapplication D/AActivity: onPause() 04-21 15:07:09.365 5151-5151/com.example.myapplication D/BActivity: onCreate() 04-21 15:07:09.366 5151-5151/com.example.myapplication D/BActivity: onStart() 04-21 15:07:09.366 5151-5151/com.example.myapplication D/BActivity: onResume() 04-21 15:07:09.674 5151-5151/com.example.myapplication D/AActivity: onStop() # 启动AActivity 04-21 15:07:19.210 5151-5151/com.example.myapplication D/BActivity: onPause() 04-21 15:07:19.220 5151-5151/com.example.myapplication D/AActivity: onCreate() 04-21 15:07:19.221 5151-5151/com.example.myapplication D/AActivity: onStart() 04-21 15:07:19.221 5151-5151/com.example.myapplication D/AActivity: onResume() 04-21 15:07:19.524 5151-5151/com.example.myapplication D/BActivity: onStop()
如果要激活的那个Activity在任务栈中存在该实例,则不需要创建,只需要把此Activity放入栈顶,并把该Activity以上的Activity实例都摧毁。
AActivity为singleTask模式,BActivity为standard模式
# 启动AActivity 04-21 15:11:55.777 5334-5334/com.example.myapplication D/AActivity: onCreate() 04-21 15:11:55.778 5334-5334/com.example.myapplication D/AActivity: onStart() 04-21 15:11:55.778 5334-5334/com.example.myapplication D/AActivity: onResume() # 启动AActivity 04-21 15:12:10.318 5334-5334/com.example.myapplication D/AActivity: onPause() 04-21 15:12:10.318 5334-5334/com.example.myapplication D/AActivity: onNewIntent() 04-21 15:12:10.318 5334-5334/com.example.myapplication D/AActivity: onResume() # 启动BActivity 04-21 15:12:14.303 5334-5334/com.example.myapplication D/AActivity: onPause() 04-21 15:12:14.316 5334-5334/com.example.myapplication D/BActivity: onCreate() 04-21 15:12:14.316 5334-5334/com.example.myapplication D/BActivity: onStart() 04-21 15:12:14.316 5334-5334/com.example.myapplication D/BActivity: onResume() 04-21 15:12:14.626 5334-5334/com.example.myapplication D/AActivity: onStop() # 启动BActivity 04-21 15:12:15.522 5334-5334/com.example.myapplication D/BActivity: onPause() 04-21 15:12:15.532 5334-5334/com.example.myapplication D/BActivity: onCreate() 04-21 15:12:15.533 5334-5334/com.example.myapplication D/BActivity: onStart() 04-21 15:12:15.533 5334-5334/com.example.myapplication D/BActivity: onResume() # 启动AActivity 04-21 15:12:15.826 5334-5334/com.example.myapplication D/BActivity: onStop() 04-21 15:12:16.599 5334-5334/com.example.myapplication D/BActivity: onDestroy()#顶层BActivity被摧毁 04-21 15:12:16.603 5334-5334/com.example.myapplication D/BActivity: onPause()#AActivity的上层BActivity被暂停,没有被立刻摧毁 04-21 15:12:16.612 5334-5334/com.example.myapplication D/AActivity: onNewIntent() 04-21 15:12:16.612 5334-5334/com.example.myapplication D/AActivity: onRestart() 04-21 15:12:16.612 5334-5334/com.example.myapplication D/AActivity: onStart() 04-21 15:12:16.612 5334-5334/com.example.myapplication D/AActivity: onResume() 04-21 15:12:16.825 5334-5334/com.example.myapplication D/BActivity: onStop() 04-21 15:12:16.825 5334-5334/com.example.myapplication D/BActivity: onDestroy()
如果应用1的任务栈中创建了MainActivity实例,如果应用2也要激活MainActivity,则不需要创建,两应用共享该Activity实例;
AActivity为singleInstance模式,BActivity为standard模式
# 启动AActivity 04-21 15:15:54.413 5475-5475/? D/AActivity: onCreate() 04-21 15:15:54.414 5475-5475/? D/AActivity: onStart() 04-21 15:15:54.414 5475-5475/? D/AActivity: onResume() # 启动AActivity 04-21 15:15:56.353 5475-5475/com.example.myapplication D/AActivity: onPause() 04-21 15:15:56.353 5475-5475/com.example.myapplication D/AActivity: onNewIntent() 04-21 15:15:56.353 5475-5475/com.example.myapplication D/AActivity: onResume() # 启动BActivity 04-21 15:15:58.695 5475-5475/com.example.myapplication D/AActivity: onPause() 04-21 15:15:58.702 5475-5475/com.example.myapplication D/BActivity: onCreate() 04-21 15:15:58.703 5475-5475/com.example.myapplication D/BActivity: onStart() 04-21 15:15:58.703 5475-5475/com.example.myapplication D/BActivity: onResume() 04-21 15:15:59.159 5475-5475/com.example.myapplication D/AActivity: onStop() # 启动BActivity 04-21 15:15:59.987 5475-5475/com.example.myapplication D/BActivity: onPause() 04-21 15:16:00.000 5475-5475/com.example.myapplication D/BActivity: onCreate() 04-21 15:16:00.002 5475-5475/com.example.myapplication D/BActivity: onStart() 04-21 15:16:00.002 5475-5475/com.example.myapplication D/BActivity: onResume() 04-21 15:16:00.308 5475-5475/com.example.myapplication D/BActivity: onStop() # 启动AActivity 04-21 15:16:01.727 5475-5475/com.example.myapplication D/BActivity: onPause() 04-21 15:16:01.728 5475-5475/com.example.myapplication D/AActivity: onNewIntent() 04-21 15:16:01.728 5475-5475/com.example.myapplication D/AActivity: onRestart() 04-21 15:16:01.729 5475-5475/com.example.myapplication D/AActivity: onStart() 04-21 15:16:01.729 5475-5475/com.example.myapplication D/AActivity: onResume() 04-21 15:16:02.191 5475-5475/com.example.myapplication D/BActivity: onStop()
复用Activity必定会调用onNewIntent,在除standard模式之外的三种模式中栈顶复用的生命周期方法调用都是onPause()、onNewIntent()、onResume()
singleTask复用不是栈顶的Activity时,会调用onNewIntent()、onRestart()、onStart(),而且会清空在要复用的Activity上层的Activity
singleInstance复用不是栈顶的Activity时,同样会调用onNewIntent()、onRestart()、onStart(),但是不会清空在要复用的Activity上层的Activity
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。