Android Activity为什么要细化出onCreate、onStart、onResume、o

原创链接:,转载请注明,谢谢。

最近在研究Activity的启动流程,老罗的blog在看,也找了其它资料学习,也跟过Android4.3的源码,

在跟代码的过程中,突然想到下面的这个问题:

Android Activity为什么要细化出onCreate、onStart、onResume、onPause、onStop、onDesdroy这么多方法让应用去重载?

网上太多根据Android开发规范翻译转载的内容,都不是我想要的答案,那就自己分析下。

如下是一段典型的Activity间切换的日志,从AActivity切换到BActivity:

10-17 20:54:42.247: I/com.example.servicetest.AActivity(5817): onCreate() 1166919192 taskID=6610-17 20:54:42.263: I/com.example.servicetest.AActivity(5817): onStart() 1166919192 taskID=6610-17 20:54:42.263: I/com.example.servicetest.AActivity(5817): onResume() 1166919192 taskID=6610-17 20:54:46.997: I/com.example.servicetest.AActivity(5817): onPause() 1166919192 taskID=6610-17 20:54:47.021: I/com.example.servicetest.BActivity(5817): onCreate() 1166971824 taskID=6610-17 20:54:47.028: I/com.example.servicetest.BActivity(5817): onStart() 1166971824 taskID=6610-17 20:54:47.028: I/com.example.servicetest.BActivity(5817): onResume() 1166971824 taskID=6610-17 20:54:47.099: I/com.example.servicetest.AActivity(5817): onStop() 1166919192 taskID=66当触发从AActivity切换到BActivity时的日志如下:

10-17 20:54:46.997: I/com.example.servicetest.AActivity(5817): onPause() 1166919192 taskID=6610-17 20:54:47.021: I/com.example.servicetest.BActivity(5817): onCreate() 1166971824 taskID=6610-17 20:54:47.028: I/com.example.servicetest.BActivity(5817): onStart() 1166971824 taskID=6610-17 20:54:47.028: I/com.example.servicetest.BActivity(5817): onResume() 1166971824 taskID=6610-17 20:54:47.099: I/com.example.servicetest.AActivity(5817): onStop() 1166919192 taskID=66

先AActivity的onPause()被调用,然后是BActivity的初始化流程(onCreate() –>onStart() –>onResume()),再然后是AActivity的onStop()被调用。

有点意思,为什么不是先AActivity的onPause()、onStop()被调用,然后再BActivity的初始化流程(onCreate() –>onStart() –>onResume())?

或者又为什么不是先BActivity的初始化流程(onCreate() –>onStart() –>onResume()),再AActivity的onPause()、onStop()被调用?

如下是Activity的几个关键方法的注释:

void android.app.Activity.onCreate(Bundle savedInstanceState)Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity’s UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri, String[], String, String[], String) to retrieve cursors for data being displayed, etc.You can call finish from within this function, in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (onStart, onResume, onPause, etc) executing.

void android.app.Activity.onStart()

Called after onCreate — or after onRestart when the activity had been stopped, but is now again being displayed to the user. It will be followed by onResume.

void android.app.Activity.onResume()

Called after onRestoreInstanceState, onRestart, or onPause, for your activity to start interacting with the user. This is a good place to begin animations, open exclusive-access devices (such as the camera), etc.

Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged to know for certain that your activity is visible to the user (for example, to resume a game).

void android.app.Activity.onPause()

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume.

When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A’s onPause returns, so be sure to not do anything lengthy here.

This callback is mostly used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one. This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.

In situations where the system needs more memory it may kill paused processes to reclaim resources. Because of this, you should be sure that all of your state is saved by the time you return from this function. In general onSaveInstanceState is used to save per-instance state in the activity and this method is used to store global persistent data (in content providers, files, etc.)

只是微笑地固执自己的坚持,

Android Activity为什么要细化出onCreate、onStart、onResume、o

相关文章:

你感兴趣的文章:

标签云: