android onPause()和onStop()区别

学了这么久的android了,对于一些基础还是有点模棱两可,比如在面试的时候遇到的onPause()和onStop()区别。之前从来没有去了解过这activity的这两个生命周期有什么区别,下来过后去看了一些文章,发现有几种不同的说法, 也不知道哪一种说法是正确的,所以只有去google API里面查了一下。总结如下,总结有误的欢迎拍砖。
Paused
谷歌官方的解释是:Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn’t cover the entire screen. A paused activity is completely alive (the Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.

另外一个activity(BActivity)在前台显示并且获取了焦点,但是之前的activity(AActivity)还是可见的。即,BActivity在AActivity的上面并且BActivity是部分透明的或者不覆盖整个屏幕。一个处于paused状态的Activity 是完全活着的,(Activity对象保留在内存中,它保持着所有的状态和成员信息并且和windows Manager保持着相连接),但可以在低内存的时候被系统杀死。
Stopped
谷歌官方的解释是:The activity is completely obscured by another activity (the activity is now in the “background”). A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.

一个activity(AActivity)完全被另一个Activity(BActivity)挡住(AActivity在后面)。一个处于Stopped状态的activity 也是任然活着的(Activity对象保留在内存中,它保持所有的状态和成员信息,但是和Stopped不同的一点就是没有和Windows Manager相连接),然而它不再对用户可见,它可以在任何需要内存的地方被杀死。

OnPause()

官方对OnPause()方法的解释如下:
Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns.
Followed either by onResume() if the activity returns back to the front, or by onStop() if it becomes invisible to the user.

当系统即将恢复另一个activity的时候,此方法通常用于提交一些未保存的持久性数据,停止动画和一些可能会消耗CPU的东西,它应该尽一切可能快的完成,因为接下来的Activity在OnPause()返回之前不会被恢复。
接下来要么运行onResume()方法(如果Activity返回到前台),要么运行onStop()方法(如果Activity变得对用户不可见)。
Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.
Followed either by onRestart() if the activity is coming back to interact with the user, or by onDestroy() if this activity is going away.

onStop()

Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.
Followed either by onRestart() if the activity is coming back to interact with the user, or by onDestroy() if this activity is going away.

当activity不再对用户可见时调用此方法,这方法可能被调用,因为它即将被销毁,或者因为另外一个Activity(可能是一个已经存在的Activity或者一个新的Activity)已经被恢复然后覆盖了这个activity。
接下来如果这个activity 即将再次与用户交互(我觉得是再次对用户可见的意思),就会调用onRestart()方法,如果这个Activity即将死亡的话,就会调用onDestroy()方法。

上面是Activities中对OnPause()和onStop()的定义和解释。
在Managing the activity lifeCycle中,对它们的描述是:
Generally, you should not use onPause() to store user changes (such as personal information entered into a form) to permanent storage. The only time you should persist user changes to permanent storage within onPause() is when you’re certain users expect the changes to be auto-saved (such as when drafting an email). However, you should avoid performing CPU-intensive work during onPause(), such as writing to a database, because it can slow the visible transition to the next activity (you should instead perform heavy-load shutdown operations during onStop()).
一般来说,我们不用该在onPause()中将例如一个表单内容保存到物理内存中,只有当你确认用户引起的变化是必须必须被自动保存的时候(例如起草一封电子邮件)。然而你应该避免在onPause()的时候做一些占用CPU很长的工作,比如写数据库,因为它会减缓下一个Activity出现的过程(意思就是要尽可能快地运行完onPause()),你应该在onStop的时候干这个事情。

你可能感兴趣的:(android,谷歌,onPause,onStop,生命周期区别)