Android Activity 与 Fragment 生命周期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23004519/
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
Activity vs Fragment Lifecycle
提问by N Sharma
I am working on new application where I am using Activity
and Fragment
. Any main difference between them ??
我正在开发我正在使用Activity
和的新应用程序Fragment
。它们之间有什么主要区别??
Update
更新
I found good answer which I wanted in Android docs.
我在 Android 文档中找到了我想要的好答案。
Docs described
文档描述
The most significant difference in lifecycle between an activity and a fragment is how one is stored in its respective back stack. An activity is placed into a back stack of activities that's managed by the system when it's stopped, by default (so that the user can navigate back to it with the Back button, as discussed in Tasks and Back Stack). However, a fragment is placed into a back stack managed by the host activity only when you explicitly request that the instance be saved by calling addToBackStack() during a transaction that removes the fragment.
Activity 和 Fragment 之间生命周期最显着的区别在于它们如何存储在各自的后台堆栈中。默认情况下,活动被放置在由系统管理的活动的返回堆栈中(以便用户可以使用返回按钮导航回它,如任务和返回堆栈中所述)。但是,仅当您在删除片段的事务期间通过调用 addToBackStack() 显式请求保存实例时,片段才会被放置到由主机活动管理的返回堆栈中。
Does host Activity
keep different back stack of different Fragment
associated with it and any scenario where your single application keep multiple stacks. ??
主机是否Activity
保留Fragment
与其相关联的不同后端堆栈以及您的单个应用程序保留多个堆栈的任何场景。??
回答by GrIsHu
Differences between Activity and Fragment lifecyle in Android
Fragment is a part of an activity, which contributes its own UI to that activity. Fragment can be thought like a sub activity. Fragments are used to efficiently use the space in wider screen devices.
Fragment 是活动的一部分,它为该活动贡献自己的 UI。Fragment 可以被认为是一个子活动。Fragments 用于有效利用更宽屏幕设备中的空间。
An activity may contain 0 or multiple number of fragments based on the screen size. A fragment can be reused in multiple activities, so it acts like a reusable component in activities.
根据屏幕大小,一个活动可能包含 0 个或多个片段。一个片段可以在多个活动中重用,因此它在活动中就像一个可重用的组件。
A fragment can't exist independently. It should be always part of an activity. Where as activity can exist with out any fragment in it.
片段不能独立存在。它应该始终是活动的一部分。活动可以在没有任何片段的情况下存在。
A fragment lifecycle is more complex than the activity lifecycle because it has more states. Lifecycle states are shown below:
片段生命周期比活动生命周期更复杂,因为它有更多的状态。生命周期状态如下所示:
onInflate
充气
At very beginning of the fragment life the method onInflate is called. In this method we can save some configuration parameter and some attributes define in the XML layout file.
在片段生命的最开始,调用方法 onInflate。在这种方法中,我们可以保存一些配置参数和一些属性定义在 XML 布局文件中。
onAttach
附加上
After this step onAttach is called. This method is called as soon as the fragment is “attached” to the “father” activity and we can this method to store the reference about the activity.
在这一步之后 onAttach 被调用。一旦片段“附加”到“父”活动,就会调用此方法,我们可以使用此方法来存储有关活动的引用。
onCreate
在创建
It is one of the most important step, our fragment is in the creation process. This method can be used to start some thread to retrieve data information, maybe from a remote server. The onCreateViewis the method called when the fragment has to create its view hierarchy.During this method we will inflate our layout inside the fragment.
这是最重要的步骤之一,我们的片段正在创建过程中。此方法可用于启动一些线程来检索数据信息,可能来自远程服务器。该onCreateView是调用的时候,片段具有创建其hierarchy.During这种方法,我们会膨胀的片段里面我们的布局视图的方法。
During this phase we can't be sure that our activity is still created so we can't count on it for some operation. We get notified when the “father” activity is created and ready in the onActivityCreated.
在这个阶段,我们不能确定我们的活动是否仍然被创建,所以我们不能指望它进行某些操作。当“父”活动在onActivityCreated 中创建并准备就绪时,我们会收到通知。
From now on, our activity is active and created and we can use it when we need.
从现在开始,我们的 Activity 处于活动状态并已创建,我们可以在需要时使用它。
onStart
开始时
The next step is onStartmethod. Here we do the common things as in the activity onStart, during this phase our fragment is visible but it isn't still interacting with the user.
下一步是onStart方法。在这里我们做一些常见的事情,就像在 onStart 活动中一样,在这个阶段我们的片段是可见的,但它仍然没有与用户交互。
onResume
在简历上
When the fragment is ready to interact with user onResumeis called.
当片段准备好与用户交互时,调用onResume。
Then it can happen that the activity is paused and so the activity's onPauseis called. Well onPausefragment method is called too.
然后可能会发生活动暂停,因此活动的onPause被调用。好吧,onPause片段方法也被调用了。
After it, it can happen that the OS decides to destroy our fragment view and so onDestroyViewis called. After it, if the system decides to dismiss our fragment it calls onDestroymethod.
之后,操作系统可能会决定销毁我们的片段视图,因此调用onDestroyView。之后,如果系统决定解除我们的片段,它会调用onDestroy方法。
Here we should release all the connection active and so on because our fragment is close to die. Even if it is during the destroy phase it is still attached to the father activity. The last step is detach the fragment from the activity and it happens when onDetachis called.
在这里,我们应该释放所有活动的连接等等,因为我们的片段快要死了。即使是在销毁阶段,它仍然依附于父活动。最后一步是从活动中分离片段,它发生在onDetach被调用时。
I hope you can understand from this.
我希望你能从中明白。
Thanks.
谢谢。
回答by My God
Directly from developer Fragments guide-
直接来自开发人员片段指南-
The lifecycle of the activity in which the fragment lives directly affects the lifecycle of the fragment, such that each lifecycle callback for the activity results in a similar callback for each fragment.
片段所在活动的生命周期直接影响片段的生命周期,因此活动的每个生命周期回调都会导致每个片段的类似回调。
For example, when the activity receives onPause()
, each fragment in the activity receives onPause()
.
例如,当活动收到 时,活动中的onPause()
每个片段都会收到onPause()
。
Fragments have a few extra lifecycle callbacks, however, that handle unique interaction with the activity in order to perform actions such as build and destroy the fragment's UI. These additional callback methods are:
Fragment 有一些额外的生命周期回调,但是,它们处理与 Activity 的独特交互,以便执行诸如构建和销毁 Fragment 的 UI 之类的操作。这些额外的回调方法是:
1)onAttach()=>
1) onAttach()=>
Called when the fragment has been associated with the activity (the Activity is passed in here).
当片段与活动相关联时调用(活动在此处传入)。
2)onCreateView()=>
2) onCreateView()=>
Called to create the view hierarchy associated with the fragment.
调用以创建与片段关联的视图层次结构。
3)onActivityCreated()=>
3) onActivityCreated()=>
Called when the activity's onCreate() method has returned.
当活动的 onCreate() 方法返回时调用。
4)onDestroyView()=>
4) onDestroyView()=>
Called when the view hierarchy associated with the fragment is being removed.
在删除与片段关联的视图层次结构时调用。
5)onDetach()=>
5) onDetach()=>
Called when the fragment is being disassociated from the activity.
当片段与活动分离时调用。
Once the activity
reaches the resumed state, you can freely add and remove fragments
to the activity
. Thus, only while the activity is in the resumed state can the lifecycle of a fragment change independently.
一旦activity
达到了恢复状态,您可以自由添加和删除fragments
的activity
。因此,只有当 Activity 处于恢复状态时,片段的生命周期才能独立变化。
However, when the activity
leaves the resumed state, the fragment
again is pushed through its lifecycle by the activity
.
然而,当activity
离开恢复状态时,fragment
再次被activity
.
Check Activity and Fragments.
回答by Naresh R
Its similar in one sense. Both gets its life cycle till they exist on the screen. If your activity is closed, it is stopped and destroyed later. Similarly Fragments gets destroyed if they are no more available in the scree. But since fragments live inside a activity, its likely that fragments are destroyed whenever some other fragment replaces this fragment. Thus it owns a similar but distinct lifecycle.
它在某种意义上是相似的。两者都有其生命周期,直到它们出现在屏幕上。如果您的活动已关闭,它将在稍后停止并销毁。同样,如果碎片在 scree 中不再可用,它们就会被销毁。但是由于片段存在于活动中,因此每当其他片段替换此片段时,片段很可能会被破坏。因此,它拥有相似但不同的生命周期。