Android活动的生命周期是什么?为什么在初始化过程中调用这么多类似的方法(onCreate(), onStart(), onResume()),而在最后调用这么多其他方法(onPause(), onStop(), onDestroy()) ?

什么时候调用这些方法,应该如何正确地使用它们?


当前回答

我按照上面的答案运行一些日志,下面是输出:

开始活动

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在每一个实例离开(除了关闭应用程序)。

其他回答

活动有六个状态

创建 开始 恢复 停顿了一下 停止 摧毁了

活动生命周期有七个方法

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()

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:

在Android开发者页面,

onPause():

当系统即将开始恢复前一个活动时调用。 这通常用于将未保存的更改提交到持久数据, 停止动画和其他可能消耗CPU的东西,等等。 这个方法的实现必须非常快,因为接下来 直到此方法返回,活动才会恢复。紧随其后的是 onResume()如果活动返回到前面,或者 onStop()如果它对用户不可见。

原():

当活动对用户不再可见时调用,因为 另一项活动已经恢复,正在处理这一事件。这可能 发生的原因可能是一个新的活动正在启动,也可能是一个现有的活动 被带到这个面前,或者这个被摧毁。 如果此活动返回,则后面跟着onRestart() 与用户交互,或者onDestroy()如果这个活动消失。

现在假设有三个活动,你从A到B,然后onPause of A将被调用,现在从B到C,然后onPause of B和onStop of A将被调用。

暂停的活动得到恢复,停止的活动得到重新启动。

当你调用this.finish()时,onPause-onStop-onDestroy将被调用。需要记住的主要事情是:当Android需要内存用于其他操作时,暂停的活动将被停止,停止的活动将被销毁。

我希望这足够清楚了。

我按照上面的答案运行一些日志,下面是输出:

开始活动

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在每一个实例离开(除了关闭应用程序)。

Android活动的生命周期是什么?

在android sdk框架中,每个android Activity(窗口)都有生命周期方法。这意味着,当用户进入一个应用程序,他可以看到在onCreate()生命周期方法中创建的活动。只在onCreate()方法中附加在窗口中的布局。

Activity(Window)有以下生命周期状态:

Create - Activity is created. 
Start - Current activity gets started.
Resume - Current activity has been in resumed state.
Restart - Current activity has been in restarted.
Pause - Current activity has been in Paused state.
Stop - Current activity has been in stopped state.
destroy - Current activity has been in destroyed state.

为什么有这么多听起来类似的方法(onCreate(), onStart(), onResume())在初始化期间调用,以及许多其他调用 (onPause(), onStop(), onDestroy())在结束时调用?

用户第一次进入应用程序:

当打开应用程序时,我们可以看到一个窗口(Activity)。onCreate (created) -> onStart(started) -> onResume(resume state)将被调用。

从后台关闭应用程序:

当从后台关闭应用程序时,必须销毁活动以释放一些内存。因此,onPause -> onStop -> onDestroy方法将被调用。

什么时候调用这些方法,应该如何正确地使用它们?

启动应用程序:

当用户第一次进入一个活动或应用程序时:

onCreate()

onStart() 

onResume()

当你从android studio运行应用程序:

onCreate()

onStart() 

onResume()

活动的转变:

当移动从第一活动->第二活动:

first_activity  : onPause()

second_activity : onCreate()

second_activity : onStart()

second_activity : onResume()

first_activity  : onStop()

当移动从第二个活动->第一个活动:

second_activity : onPause()

first_activity  : onRestart()

first_activity  : onStart()

first_activity  : onResume()

second_activity : onStop()

second_activity : onDestroy()

概述按钮:

当用户点击概述按钮(硬件第三个按钮-最近列表):

onPause()

onStop()

当用户关闭概览按钮(或)后,用户从最近的列表中进入其他应用程序,并返回到应用程序:

onRestart()

onStart()

onResume()

主页按钮:

当用户点击Home键时:

onPause()

onStop()

用户搜索主屏幕,点击应用程序图标回到活动:

onRestart()

onStart()

onResume()

用户收到电话:

当用户在一个活动,电话来了:

onPause()

onStop()

如果用户没有参加呼叫,它会自动断开并返回活动(未接呼叫):

onRestart()

onStart()

onResume()

如果用户不参加呼叫:

-不调用任何生命周期。

关机按钮:

当用户关闭按钮时:

onPause()

onStop()

解锁设备时:

onRestart()

onStart()

onResume()

弹出对话框:

当弹出对话框出现-没有生命周期将被调用

重启设备或关闭开关:

用户重启或关闭设备时:

onPause()

onStop()

当用户从主屏幕点击应用程序图标时:

onCreate()

onStart()

onResume()