Android活动的生命周期是什么?为什么在初始化过程中调用这么多类似的方法(onCreate(), onStart(), onResume()),而在最后调用这么多其他方法(onPause(), onStop(), onDestroy()) ?
什么时候调用这些方法,应该如何正确地使用它们?
Android活动的生命周期是什么?为什么在初始化过程中调用这么多类似的方法(onCreate(), onStart(), onResume()),而在最后调用这么多其他方法(onPause(), onStop(), onDestroy()) ?
什么时候调用这些方法,应该如何正确地使用它们?
当前回答
造成整个混乱的原因是谷歌选择了非直观的名称,而不是如下所示:
onCreateAndPrepareToDisplay() [instead of onCreate() ]
onPrepareToDisplay() [instead of onRestart() ]
onVisible() [instead of onStart() ]
onBeginInteraction() [instead of onResume() ]
onPauseInteraction() [instead of onPause() ]
onInvisible() [instead of onStop]
onDestroy() [no change]
活动图可以解释为:
其他回答
活动有六个状态
创建 开始 恢复 停顿了一下 停止 摧毁了
活动生命周期有七个方法
onCreate () onStart () onResume () onPause () 原() onRestart () onDestroy ()
图源
的情况下
When open the app onCreate() --> onStart() --> onResume() When back button pressed and exit the app onPaused() -- > onStop() --> onDestory() When home button pressed onPaused() --> onStop() After pressed home button when again open app from recent task list or clicked on icon onRestart() --> onStart() --> onResume() When open app another app from notification bar or open settings onPaused() --> onStop() Back button pressed from another app or settings then used can see our app onRestart() --> onStart() --> onResume() When any dialog open on screen onPause() After dismiss the dialog or back button from dialog onResume() Any phone is ringing and user in the app onPause() --> onResume() When user pressed phone's answer button onPause() After call end onResume() When phone screen off onPaused() --> onStop() When screen is turned back on onRestart() --> onStart() --> onResume()
我按照上面的答案运行一些日志,下面是输出:
开始活动
On Activity Load (First Time)
————————————————————————————————————————————————
D/IndividualChatActivity: onCreate:
D/IndividualChatActivity: onStart:
D/IndividualChatActivity: onResume:
D/IndividualChatActivity: onPostResume:
Reload After BackPressed
————————————————————————————————————————————————
D/IndividualChatActivity: onCreate:
D/IndividualChatActivity: onStart:
D/IndividualChatActivity: onResume:
D/IndividualChatActivity: onPostResume:
OnMaximize(Circle Button)
————————————————————————————————————————————————
D/IndividualChatActivity: onRestart:
D/IndividualChatActivity: onStart:
D/IndividualChatActivity: onResume:
D/IndividualChatActivity: onPostResume:
OnMaximize(Square Button)
————————————————————————————————————————————————
D/IndividualChatActivity: onRestart:
D/IndividualChatActivity: onStart:
D/IndividualChatActivity: onResume:
D/IndividualChatActivity: onPostResume:
停止活动
On BackPressed
————————————————————————————————————————————————
D/IndividualChatActivity: onPause:
D/IndividualChatActivity: onStop:
D/IndividualChatActivity: onDestroy:
OnMinimize (Circle Button)
————————————————————————————————————————————————
D/IndividualChatActivity: onPause:
D/IndividualChatActivity: onStop:
OnMinimize (Square Button)
————————————————————————————————————————————————
D/IndividualChatActivity: onPause:
D/IndividualChatActivity: onStop:
Going To Another Activity
————————————————————————————————————————————————
D/IndividualChatActivity: onPause:
D/IndividualChatActivity: onStop:
Close The App
————————————————————————————————————————————————
D/IndividualChatActivity: onDestroy:
在我个人看来,onStart和onStop只需要两个。
onResume似乎在每一个实例回来,和onPause在每一个实例离开(除了关闭应用程序)。
安卓系统生命周期
有7种方法可以管理Android应用程序的生命周期:
onCreate () onStart () onResume () onRestart () onPause () 原() onDestroy ()
回答所有这些方法都是为了什么:
让我们以一个简单的场景为例,在这个场景中,了解这些方法的调用顺序将有助于我们清楚地了解为什么要使用它们。
假设你正在使用一个计算器应用程序。调用了三个方法 连续启动应用程序。
on开始()
当我正在使用计算器应用程序时,突然一个电话来了。 计算器活动转到后台,另一个活动说。 对调用的处理出现在前台,现在有两个方法 连续呼叫。
onPause() - - - > onStop()
现在假设我完成了电话上的对话,计算器 Activity从后台进入前台,有三种方法 依次调用。
重新启动()- - - > onStart() - - - - > on简历()
最后,假设我已经完成了计算器应用程序中的所有任务,并且我 想要退出应用程序。进一步的两个方法被连续调用。
连续()- - - >致残()
一个活动可能存在四种状态:
开始状态 运行状态 暂停状态 停止状态
起始状态包括:
创建一个新的Linux进程,为新的UI对象分配新的内存,并设置整个屏幕。大部分的工作都在这里。
运行状态包括:
它是当前在屏幕上的活动(状态)。这个状态单独处理在屏幕上输入、触摸和点击按钮等事情。
暂停状态包括:
当一个活动不在前台,而是在后台时,该活动被称为处于暂停状态。
停止状态包括:
一个停止的活动只能通过重新启动才能被买到前台,而且它可以在任何时间点被销毁。
活动管理器以这样一种方式处理所有这些状态,即使在将新活动添加到现有活动的场景中,用户体验和性能也始终处于最佳状态
I like this question and the answers to it, but so far there isn't coverage of less frequently used callbacks like onPostCreate() or onPostResume(). Steve Pomeroy has attempted a diagram including these and how they relate to Android's Fragment life cycle, at https://github.com/xxv/android-lifecycle. I revised Steve's large diagram to include only the Activity portion and formatted it for letter size one-page printout. I've posted it as a text PDF at https://github.com/code-read/android-lifecycle/blob/master/AndroidActivityLifecycle1.pdf and below is its image:
造成整个混乱的原因是谷歌选择了非直观的名称,而不是如下所示:
onCreateAndPrepareToDisplay() [instead of onCreate() ]
onPrepareToDisplay() [instead of onRestart() ]
onVisible() [instead of onStart() ]
onBeginInteraction() [instead of onResume() ]
onPauseInteraction() [instead of onPause() ]
onInvisible() [instead of onStop]
onDestroy() [no change]
活动图可以解释为: