本文主要是介绍安卓Activity之生命周期,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
看了Scott的博客,不如自己去动手。这样印象更加深刻。方便以后更加灵活的运用。
- onCreate:界面创建时调用
- onStart:界面处于可见状态时调用
- onResume:界面能够操作有焦点时调用
- onPause:界面不在Activity顶层,但依然可见
- onStop:界面处于不可见状态时调用,此时没有焦点
- onDetory:界面退出状态时调用,并销毁
- onSaveInstanceState:当界面进行屏幕旋转时调用,保存当前界面的数据
- onRestoreInstanceState:当界面旋转后,重新创建的过程中调用,使用该方法获取保存的原始数据
A界面与B界面的启动流程
- 启动A界面流程:onCreate——>onStart——>onResume
- 从A界面跳转到B流程:(A)onPause——>(B)onCreate——>(B)onStart——>(B)onResume——>(A)onStop
- 从B界面返回到A流程:(B)onPause——>(A)onRestart——>(A)onStart——>(A)onResume——>(B)onStop——>(B)onDeatory
- 界面锁屏或按Home的流程:onPause——>onSaveInstanceState——>onStop
- 界面解锁或按Home的流程:onRestart——>onStart——>onResume
- 屏幕旋转启动流程:onPause——>onSaveInstanceState——>onStop——>onDestroy——>onCreate——>onStart——>onRestoreInstanceState——>onResume
package com.zy.gathersamples.activity;import android.content.Intent;
import android.util.Log;
import android.view.View;
import com.zy.gathersamples.R;public class MainActivity extends Activity {private String TAG = "MainActivity";@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);Log.e(TAG, "onSaveInstanceState");}@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {super.onRestoreInstanceState(savedInstanceState);Log.e(TAG, "onRestoreInstanceState");}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.e(TAG, "onCreate");}@Overrideprotected void onStart() {super.onStart();Log.e(TAG, "onStart");}@Overrideprotected void onStop() {super.onStop();Log.e(TAG, "onStop");}@Overrideprotected void onDestroy() {super.onDestroy();Log.e(TAG, "onDestroy");}@Overrideprotected void onPause() {super.onPause();Log.e(TAG, "onPause");}@Overrideprotected void onResume() {super.onResume();Log.e(TAG, "onResume");}@Overrideprotected void onRestart() {super.onRestart();Log.e(TAG, "onRestart");}
}
这篇关于安卓Activity之生命周期的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!