如何控制片段的顺序加载在我的应用程序 [英] How to control the order of the fragments being loaded in my app

查看:137
本文介绍了如何控制片段的顺序加载在我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图嵌入片段的API应用程序,但我无法控制所加载片段的顺序,从而获得一些例外。 配置我的布置是这样的:

I'm trying to embed the fragments API to an application but I can't control the order of fragments being loaded and thus getting some exceptions. I configure my layout this way:

 <fragment class="org.me.myListFragment"
        android:id="@+id/frag_title"

        android:layout_marginTop="20dp"
        android:layout_width="@dimen/titles_size"
        android:layout_height="match_parent" 
        android:layout_weight="1"
        />
 <fragment class="org.me.DetailsFragment"
        android:id="@+id/details"

        android:layout_marginTop="20dp"
        android:layout_width="@dimen/titles_size"
        android:layout_height="match_parent" android:layout_weight="1"

        />

</FrameLayout>

但DetailsFragment被列表中的片段之前加载,这将导致trubles。 我不想打破旧的code,控制顺序这里将是解决这个问题的最快方法。 有没有一种办法可以让第二个片段后,才第一次被初始化?

But the DetailsFragment is being loaded before the list fragment and this causes trubles. I don't want to break the old code, controlling the order here would be the quickest way to fix it. Is there a way I can make the second fragment to be initialized only after the first one?

推荐答案

没有,你可以,如果你使用的不是控制命令&LT;片断&gt; 元素。如果切换到动态片段(例如, FragmentTransaction ),您可以控制​​创建片段的时候,因为你创建它们。

No, you cannot control the order if you use <fragment> elements. If you switch to dynamic fragments (e.g., FragmentTransaction), you control when fragments are created, because you create them.

恕我直言,一个片段不应该依赖于在您所描述的方式另一个片段。

IMHO, one fragment should not depend upon another fragment in the manner that you describe.

这篇关于如何控制片段的顺序加载在我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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