功放加之间的差异();替换()与片段的生命周期 [英] Difference between add() & replace() with Fragment's lifecycle

查看:189
本文介绍了功放加之间的差异();替换()与片段的生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计划有6个片段:片段1,Fragment2,....-> Fragment6

我使用add()和replace()语句和片段之间切换,跟踪他们的生命周期。

片段1的添加 Fragment2的添加 Fragment3的添加 Fragment4的添加 Fragment5的替换 Fragment6

日志-猫显示出其生命周期(我在的onCreate,onCreateView,onDestroyView,为的onDestroy一些跟踪的printf点)


代码 的__ _ __ _ __ _ __ _ __ 文字

Fragment1_ _ __ _ 的___ _onCreate

Fragment1_ _ __ _ 的___ _onCreateView

Fragment1_ _ __ _ 的___ _add Fragment2

Fragment2_ _ __ _ 的___ _onCreate

Fragment2_ _ __ _ 的___ _onCreateView

Fragment2_ _ __ _ 的___ _add Fragment3

Fragment3_ _ __ _ 的___ _onCreate

Fragment3_ _ __ _ 的___ _onCreateView

Fragment3_ _ __ _ 的___ _add Fragment4

Fragment4_ _ __ _ 的___ _onCreate

Fragment4_ _ __ _ 的___ _onCreateView

Fragment4_ _ __ _ 的___ _add Fragment5

Fragment5_ _ __ _ 的___ _onCreate

Fragment5_ _ __ _ 的___ _onCreateView

Fragment5 的__ _ __ _ _ 替换Fragment6

片段1 的__ _ __ _ _ onDestroyView

Fragment3 的__ _ __ _ _ onDestroyView

Fragment5 的__ _ __ _ _ onDestroyView

Fragment6_ _ __ _ 的___ _onCreate

Fragment6_ _ __ _ 的___ _onCreateView


我的问题:

为什么后Fragment5是的替换按Fragment6,该片段1&放大器; 3及?5摧毁了他们的看法。

什么是Fragment2&放happending; 4?

为什么 Fragment2&安培; 4不被破坏他们作为片段1&放图。 3及?5

请帮我充分了解有关片段的生命周期调用add(时)和replace()方法。


更新我的addFragment和replaceFragment方式:

 公共无效addFragment(片段fromFragment,片段toFragment){
    FragmentManager经理= getFragmentManager();
    FragmentTransaction交易= manager.beginTransaction();
    transaction.add(R.id.container,toFragment,toFragment.getClass()的getName());
    transaction.hide(fromFragment);
    transaction.addToBackStack(toFragment.getClass()的getName());
    器transaction.commit();
}公共无效replaceFragment(片段fromFragment,片段toFragment){
    FragmentManager经理= getFragmentManager();
    FragmentTransaction交易= manager.beginTransaction();
    transaction.replace(R.id.container,toFragment,toFragment.getClass()的getName());
    transaction.hide(fromFragment);
    transaction.addToBackStack(toFragment.getClass()的getName());
    器transaction.commit();
}


解决方案

当您更换,你换了所有的片段(1,2,3,4,5)的的ViewGroup R.id.container 为您的新片段(6)。一旦片段已被删除,他们将被破坏。当被破坏,他们将调用 onDestroyView()方法。

<一个href=\"http://developer.android.com/reference/android/app/FragmentTransaction.html#replace(int,%20android.app.Fragment,%20java.lang.String)\"相对=nofollow> FragmentTransaction替换

至于你的问题2和4,我不知道。你能发布更多的code写什么到LogCat中的?

My program has 6 fragments: Fragment1, Fragment2,....->Fragment6.

I use the add() and replace() statement to switch between the fragment and track their lifecycle.

Fragment1 add Fragment2 add Fragment3 add Fragment4 add Fragment5 replace Fragment6

The log-cat to shown their lifecycle (i have some printf-points in onCreate, onCreateView, onDestroyView, onDestroy for tracking)


Tag ______________ Text

Fragment1_________onCreate

Fragment1_________onCreateView

Fragment1_________add Fragment2

Fragment2_________onCreate

Fragment2_________onCreateView

Fragment2_________add Fragment3

Fragment3_________onCreate

Fragment3_________onCreateView

Fragment3_________add Fragment4

Fragment4_________onCreate

Fragment4_________onCreateView

Fragment4_________add Fragment5

Fragment5_________onCreate

Fragment5_________onCreateView

Fragment5 _______ replace Fragment6

Fragment1 _______ onDestroyView

Fragment3 _______ onDestroyView

Fragment5 _______ onDestroyView

Fragment6_________onCreate

Fragment6_________onCreateView


My questions:

Why after the Fragment5 is replaced by Fragment6, the Fragment1 & 3 &5 are destroyed their view ?.

What is happending with Fragment2 & 4 ?

Why Fragment2 & 4 are not destroyed their view as Fragment1 & 3 &5 ?

Please help me to understand fully about fragment's lifecycle when call the add() and replace() method.


Update my addFragment and replaceFragment method:

public void addFragment(Fragment fromFragment, Fragment toFragment) {
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.container,toFragment, toFragment.getClass().getName());
    transaction.hide(fromFragment);
    transaction.addToBackStack(toFragment.getClass().getName());
    transaction.commit();
}

public void replaceFragment(Fragment fromFragment, Fragment toFragment) {
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.container,toFragment, toFragment.getClass().getName());
    transaction.hide(fromFragment);
    transaction.addToBackStack(toFragment.getClass().getName());
    transaction.commit();
}

解决方案

When you replace, you're swapping out all of the fragments (1,2,3,4,5) in the ViewGroup R.id.container for your new Fragment (6). Once the fragments have been removed, they will be destroyed. When destroyed, they will call the onDestroyView() method.

FragmentTransaction replace

With regards to your question for 2 and 4, I'm not sure. Are you able to post more of the code what writes out to logcat?

这篇关于功放加之间的差异();替换()与片段的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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