按下返回按钮后 Android Activity 的生命周期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22011751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Life cycle of Android Activity after pressing Back button
提问by AJ.
I am little confused between the life cycle of two activities.
我对两个活动的生命周期有点困惑。
Suppose I have Activity A and Activity B.
假设我有活动 A 和活动 B。
B is called From A i.e A ----> B
.
B 被称为来自 A 即A ----> B
。
Now currently B
is on the screen and I pressed back button. Here I want know:- is there any memory still available for B
(Active) or B
's memory is flushed(Inactive).
现在当前B
在屏幕上,我按下了后退按钮。在这里我想知道:- 是否还有任何内存可用于B
(活动)或B
的内存已刷新(非活动)。
回答by Jitesh Upadhyay
Suppose there is an activity A, from which you launch activity B. If, while in activity B, you hit the back button, you are popping activity B off the stack and B will not be in the activity stack any longer.
假设有一个活动 A,您从中启动活动 B。如果在活动 B 中,您点击后退按钮,您将活动 B 从堆栈中弹出,并且 B 将不再位于活动堆栈中。
Whenever you push an activity to the stack, onCreate
is called, and if you press back button, onDestroy
is called, which means that the activity is flushed away.
每当您将活动推入堆栈时,onCreate
就会被调用,如果您按下后退按钮,onDestroy
则会被调用,这意味着该活动将被清除。
Please visit my blog for further information: http://upadhyayjiteshandroid.blogspot.in/2013/02/android-lifecycle.html
请访问我的博客了解更多信息:http: //upadhyayjiteshandroid.blogspot.in/2013/02/android-lifecycle.html
please visit for more
请访问更多
http://developer.android.com/guide/components/tasks-and-back-stack.html
http://developer.android.com/guide/components/tasks-and-back-stack.html
http://developer.android.com/training/basics/activity-lifecycle/starting.html
http://developer.android.com/training/basics/activity-lifecycle/starting.html
回答by Annada
The following activity call back methods are called, after pressing back button.
按下后退按钮后,将调用以下活动回调方法。
onPause()
onStop()
onDestroy()
The activity is destroyed.
活动被破坏。
And it recreates when launched again. These are the callback methods when it launches again.
再次启动时它会重新创建。这些是再次启动时的回调方法。
onCreate()
onStart()
onResume()
回答by SMR
The onDestroy
method is called after back press. Then activity will be popped from the Activity back stack.
onDestroy
回按后调用该方法。然后 Activity 将从 Activity 返回堆栈中弹出。
If an activity is paused or stopped, the system can drop the activity from memoryby either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
如果活动暂停或停止,系统可以通过要求其完成或简单地终止其进程来从内存中删除该活动。当它再次显示给用户时,它必须完全重新启动并恢复到以前的状态。
onDestroy()
from docs:
onDestroy()
来自文档:
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called
finish()
on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with theisFinishing()
method.
在您的活动被销毁之前您收到的最后一个电话。这可能是因为 Activity 正在完成(有人调用
finish()
它,或者因为系统正在临时销毁该 Activity 实例以节省空间)。您可以使用isFinishing()
方法区分这两种情况。
回答by DAS
I know the answer is been accepcted, still if this helps someone I am putting it.
我知道答案已被接受,如果这对我提出的人有所帮助。
When app is opening for the first time, by clicking the Icon
当应用程序第一次打开时,通过单击图标
onCreate()
onStart()
onResume()
When home button is pressed
当home键被按下时
onPause()
onStop()
when app is again opened by clicking the app icon or launched from recent
当应用程序通过单击应用程序图标再次打开或从最近启动时
onRestart()
onStart()
onResume()
when app is opened and then back button is pressed
当应用程序打开然后按下后退按钮时
onPause()
onStop()
onDestroy()
回答by Rakesh
Activity B will be destroyed and will no longer remain in memory.
Activity B 将被销毁并且不再保留在内存中。
For more information please visit the official documentation for androidand have a look at the activity life cycle figure.
有关更多信息,请访问android的官方文档并查看活动生命周期图。
Once you press the back key the activity's onDestroy() method will be called and the activity will be flushed out of the memory. You will then be required to restart the activity by calling the startActivity() method which will in turn call its onCreate() Method.
一旦按下后退键,活动的 onDestroy() 方法将被调用,活动将从内存中清除。然后,您将需要通过调用 startActivity() 方法重新启动活动,该方法将依次调用其 onCreate() 方法。
回答by Harshal Benake
I would suggest to refer following link for activity lifecycle
我建议参考以下活动生命周期的链接
http://stackoverflow.com/a/8516056/3110609
and following link for launch mode of activity.
以及以下活动启动模式的链接。
www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode
回答by steven0529
After pressing the back button, Activity B will b destroyed. You see, Android Manages Activities like a Stack(an explanation of a stack). Everytime you start an activity, it pushes into the Activity Stack. So when Activity A calls Activity B, Activity B is now on top of Activity B, and when you press the back button, it also does a pop in the Activity Stack. So in concept, Activity B is gone. Pressing a Home Button is different from pressing back, it pauses the Activity, therefore it still eats a little of the phone's memory.
按下后退按钮后,Activity B 将被销毁。你看,Android 像堆栈一样管理活动(堆栈的解释)。每次启动活动时,它都会推送到活动堆栈中。所以当 Activity A 调用 Activity B 时,Activity B 现在在 Activity B 的顶部,当你按下后退按钮时,它也会在 Activity Stack 中弹出。所以在概念上,Activity B 已经消失了。按下 Home 键与按下返回不同,它会暂停 Activity,因此它仍然会占用手机的一些内存。
Hereis a good explanation of how Android Manages Activities.
这里很好地解释了 Android 如何管理活动。