活动VS片段生命周期 [英] Activity vs Fragment Lifecycle

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

问题描述

我对新的应用程序的工作,我现在用活动片段。它们之间的任何主要区别?

更新

我发现了,我想在Android的文档很好的答案。

文档描述

  

的活性和片段之间在生命周期中的最显著差别是如何一个存储在其各自的后栈。一个活动被放置到的活动真实由系统时,它的停止管理的回栈,由缺省值(以便用户能够导航回到它与返回按钮,如在各种工作和返回堆栈讨论)。然而,一个片段被放入一个由主机活动仅当明确请求的实例将保存由去除该片段在事务处理期间调用addToBackStack()管理的背面叠

难道主机活动保持不同的片段与它和任何情况下,你的单个应用程序保持关联的不同背叠多个栈。 ??

解决方案

  Android中的活动和片段生命周期之间的差异
 

片段是一种活动,这有助于其自己的用户界面,以该活动的一部分。片段可以被认为像一个子活性。 片段被用来有效地利用空间更宽的屏幕的​​设备。

这是活性可以包含0或多个号码基于屏幕尺寸的片段。片段可以在多个活动中重复使用,因此它的功能类似于活动的一个可重用的组件。

一个片段,不能独立存在。它应该是始终活动的一部分。凡为活动可以在它存在的任何片段。

一个片段的生命周期比活动的生命周期,因为它有更多的国家更加复杂。生命周期状态如下所示:

onInflate

目前的生活片段方法onInflate被称为最开始。在这种方法中,我们可以节省一些配置参数和一些属性在XML布局文件中定义。

onAttach

在此步骤onAttach被调用。这种方法只要片段附加到父亲的活动就叫做,我们可以这个方法来存储有关活动的基准。

的onCreate

有最重要的步骤之一,我们的片段是在创作过程。此方法可用于启动一些线程来检索数据的信息,也许从远程服务器。在 onCreateView 被调用的时候,片段具有创建视图hierarchy.During这种方法,我们会夸大我们的布局片段内的方法。

在这个阶段,我们不能肯定,我们的活动仍在创建的,因此,我们不能指望它的一些操作。我们得到通知的父亲的活动是创建并准备在 onActivityCreated 的时候。

从现在开始,我们的活动是积极和创造,我们可以使用它时,我们所需要的。

ONSTART

下一步 ONSTART 方式。在这里,我们做了共同的东西如在活动ONSTART,在此阶段我们的片段是可见的,但它是不是还与用户交互。

onResume

在片段愿与用户进行交互的 onResume 被调用。

然后,它可以发生,该活动被暂停等活动的的onPause 被调用。那么的onPause 片段方法被调用了。

它之后,它可能发生在OS决定摧毁我们的片段观等的 onDestroyView 被调用。这之后,如果系统决定驳回我们的片段,它调用的的onDestroy 方式。

在这里,我们要释放所有的连接活动等,因为我们的片段是接近死亡。即使是在破坏阶段它仍然附着在父亲的活动。最后一步是分离的活动片段,它发生在 onDetach 被调用。

我希望你可以从这个理解。

感谢。

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

Update

I found good answer which I wanted in Android docs.

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.

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 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.

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

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

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

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.

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.

From now on, our activity is active and created and we can use it when we need.

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

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

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

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.

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.

I hope you can understand from this.

Thanks.

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

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