Android 生命周期笔记

浅淡android生命周期


- 学习在于总结,归纳,方能更好的吸收。遇到问题,马上记录。各位看官大神敬请指教!


  • 生命周期官方这样说的:
    As a user navigates through, out of, and back to your app, the Activity instances in your app transition between different states in their lifecycle. For instance, when your activity starts for the first time, it comes to the foreground of the system and receives user focus. During this process, the Android system calls a series of lifecycle methods on the activity in which you set up the user interface and other components. If the user performs an action that starts another activity or switches to another app, the system calls another set of lifecycle methods on your activity as it moves into the background (where the activity is no longer visible, but the instance and its state remains intact)
    Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. For example, if you’re building a streaming video player, you might pause the video and terminate the network connection when the user switches to another app. When the user returns, you can reconnect to the network and allow the user to resume the video from the same spot.
  • 为了方便大家看我是直接翻译:
    当用户浏览,出来,并回到您的应用程序,在其生命周期的不同状态之间您的应用程序过渡的活动情况。例如,当您的活动开始的第一次,它涉及到该系统的前景,并接收用户的焦点。在此过程中,Android系统调用了一系列在其中设置了用户界面和其他成分的活性生命周期方法。如果用户进行启动另一项活动,或切换到另一个应用程序的操作时,系统调用另一组的生命周期方法对你的活动,因为它移动到背景(其中活动将不再可见,但实例和其状态保持不变)
    在生命周期回调方法,你可以宣布你是如何活动的行为,当用户离开并重新进入活动。例如,如果你正在构建一个流媒体视频播放器,你可以暂停视频,当用户切换到另一个应用程序终止网络连接。当用户返回时,则可以重新连接到网络,并允许用户从同一点恢复视频

1、 随便创建一android应用。重写下面的方法

 @Override
    protected void onStart() {
        Log.e("onTest", "onStart");
        super.onStart();
    }
    @Override
    protected void onRestart() {
        Log.e("onTest", "onRestart");
        super.onRestart();
    }
    @Override
    protected void onResume() {
        Log.e("onTest", "onResume");
        super.onResume();
    }
    @Override
    protected void onPause() {
        Log.e("onTest", "onPause");
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        Log.e("onTest", "onDestroy");
        super.onDestroy();
    }
    @Override
    protected void onStop() {
        Log.e("onTest", "onStop");
        super.onStop();
    }

2 、运行你的应用查看结果

2-1: 打开应用

log打印如下:
11-15 11:14:02.147 8182-8182/com.justdo.iiii E/onTest: onCreate
11-15 11:14:02.163 8182-8182/com.justdo.iiii E/onTest: onStart
11-15 11:14:02.171 8182-8182/com.justdo.iiii E/onTest: onResume
执行顺序 — onCreate —> onStart —> onResume

2-2:home键返回桌面&&进入其他Activity

log打印如下:
11-15 11:16:24.295 8182-8182/com.justdo.iiii E/onTest: onPause
11-15 11:16:25.835 8182-8182/com.justdo.iiii E/onTest: onStop
执行顺序 — onPause—> onStop

2-3:再次进入

log打印如下:
11-15 11:23:35.435 8182-8182/com.justdo.iiii E/onTest: onRestart
11-15 11:23:35.435 8182-8182/com.justdo.iiii E/onTest: onStart
11-15 11:23:35.435 8182-8182/com.justdo.iiii E/onTest: onResume
执行顺序 — onRestart—> onStart —> onResume

2-3: 销毁当前应用

log打印如下:
11-15 11:35:35.955 20218-20218/com.justdo.iiii E/onTest: onPause
11-15 11:35:36.519 20218-20218/com.justdo.iiii E/onTest: onStop
11-15 11:35:36.519 20218-20218/com.justdo.iiii E/onTest: onDestroy
执行顺序 — onPause—> onStop—> onDestroy

结束 ,希望能帮到你,更希望有大神指教!

你可能感兴趣的:(学习笔记)