Activity 与 Fragment 生命周期 [英] Activity vs Fragment Lifecycle

查看:35
本文介绍了Activity 与 Fragment 生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用 ActivityFragment 的新应用程序.它们之间有什么主要区别??

I am working on new application where I am using Activity and Fragment. Any main difference between them ??

我在 Android 文档中找到了我想要的好答案.

I found good answer which I wanted in Android docs.

文档描述

Activity 和 Fragment 之间生命周期最显着的区别在于它们如何存储在各自的后台堆栈中.默认情况下,活动被放置到由系统管理的活动的返回堆栈中(以便用户可以使用返回按钮导航回它,如任务和返回堆栈中所述).但是,仅当您在删除片段的事务期间通过调用 addToBackStack() 显式请求保存实例时,片段才会被放置到由主机活动管理的返回堆栈中.

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 的不同后台堆栈,以及您的单个应用程序保留多个堆栈的任何场景.??

Does host Activity keep different back stack of different Fragment associated with it and any scenario where your single application keep multiple stacks. ??

推荐答案

 Differences between Activity and Fragment lifecyle in Android

Fragment 是 Activity 的一部分,它为该 Activity 贡献自己的 UI.Fragment 可以被认为是一个子活动.Fragment 用于有效利用宽屏设备的空间.

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.

一个 Activity 可能包含 0 个或多个片段,具体取决于屏幕大小.一个片段可以在多个活动中重复使用,所以它就像活动中的一个可重复使用的组件.

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.

片段不能独立存在.它应该始终是活动的一部分.活动可以在没有任何片段的情况下存在.

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

在片段生命的最开始,调用方法 onInflate.在这种方法中,我们可以保存一些配置参数和一些属性定义在 XML 布局文件中.

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.

onAttach

在这一步之后 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.

onCreate

这是最重要的步骤之一,我们的片段在创建过程中.此方法可用于启动一些线程来检索数据信息,可能来自远程服务器.onCreateView 是在片段必须创建其视图层次结构时调用的方法.在此方法中,我们将在片段内扩展我们的布局.

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 onCreateView is the method called when the fragment has to create its view hierarchy.During this method we will inflate our layout inside the fragment.

在此阶段,我们无法确定我们的 Activity 是否仍在创建,因此我们不能指望它进行某些操作.当父"活动在 onActivityCreated 中创建并准备就绪时,我们会收到通知.

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.

从现在开始,我们的 Activity 处于活动和创建状态,我们可以在需要时使用它.

onStart

下一步是onStart 方法.在这里,我们做一些常见的事情,就像在 onStart 活动中一样,在这个阶段,我们的片段是可见的,但它仍然没有与用户交互.

The next step is onStart method. 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.

onResume

当片段准备好与用户交互时 onResume 被调用.

When the fragment is ready to interact with user onResume is called.

然后可能会发生活动暂停,因此活动的 onPause 被调用.onPause 片段方法也被调用.

Then it can happen that the activity is paused and so the activity’s onPause is called. Well onPause fragment method is called too.

之后,操作系统可能会决定销毁我们的片段视图,因此 onDestroyView 被调用.之后,如果系统决定关闭我们的片段,它会调用 onDestroy 方法.

After it, it can happen that the OS decides to destroy our fragment view and so onDestroyView is called. After it, if the system decides to dismiss our fragment it calls onDestroy method.

这里我们应该释放所有活动的连接等等,因为我们的片段快要死了.即使是在销毁阶段,它仍然依附于父活动.最后一步是从活动中分离片段,它发生在 onDetach 被调用时.

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 onDetach is called.

希望你能理解.

谢谢.

这篇关于Activity 与 Fragment 生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆