FragmentTransaction加()行为 [英] FragmentTransaction add() behavior

查看:102
本文介绍了FragmentTransaction加()行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与片段我一直使用更换()我的交易,但我想我没有保存实例状态再恢复片段的观点和工作prevent回来的片段时重新加载。所以,我决定与工作增加()。问题是当我添加另一个片段中,previous片段观点一直在后台运行,这就是罚款(这是我期望的行为),但问题是,我其实可以在后台的意见交流。 示例:

片段A 有一个按钮

片段B 有一个的TextView

当我添加的片段A 并在以后添加的片段B ,我可以点击片段A的 按钮,甚至停留在片段B的查看。

我使用的是:

  FragmentTransaction fragmentTransaction =
            getSupportFragmentManager()的BeginTransaction()。
            添加(getRootViewContainer(),片段,fragment.getClass()getSimpleName());

如果(shouldGoBack)
    fragmentTransaction.addToBackStack(fragment.getClass()getSimpleName());
 

其中, getRootViewContainer()返回 ID 的FrameLayout 我使用因为我的活动主容器。

现在,是不是真的的默认行为增加()

如果有,是否有一个适当的方式来避免这种或者一个只拥有使用更换()

解决方案

  FragmentTransaction.hide(fragmentBehind); //我的作品!
 

例如:

  //我有它全局可用
FragmentTransaction反= MainActivity.getManager()的BeginTransaction()。

//不是全局
FragmentTransaction反= getFragmentManager()的BeginTransaction()。
MapFragment newFragment =新newFragment();
trans.add(R.id.fragmentContainer,newFragment,标签);
trans.hide(本);
trans.addToBackStack(标签);
trans.commit();
 

Working with fragments I've always used replace() for my transactions, but I wish I didn't have to save instance states anymore to restore a fragment's view and prevent reloading when coming back to that fragment. So, I've decided to work with add(). The thing is when I add another fragment, the previous fragment view remains in the background and that's fine (that's the behavior I expected), but the problem is I can actually interact with the views in the background. Example:

Fragment A has a Button

Fragment B has a TextView

When I add Fragment A and later add Fragment B, I'm able to click on Fragment A's Button, even staying on Fragment B's view.

I'm using:

FragmentTransaction fragmentTransaction = 
            getSupportFragmentManager().beginTransaction().
            add(getRootViewContainer(),fragment,fragment.getClass().getSimpleName());

if (shouldGoBack) 
    fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());

where getRootViewContainer() returns the id of the FrameLayout I'm using as my activity main container.

Now, is it really the default behavior of add()?

If so, is there a proper way to avoid this or one just has to use replace()?

解决方案

FragmentTransaction.hide(fragmentBehind); //works for me!

example :

//I have it globally available
FragmentTransaction trans = MainActivity.getManager().beginTransaction(); 

//not globally 
FragmentTransaction trans = getFragmentManager().beginTransaction();
MapFragment newFragment = new newFragment();
trans.add(R.id.fragmentContainer, newFragment, tag);
trans.hide(this);
trans.addToBackStack(tag);
trans.commit();

这篇关于FragmentTransaction加()行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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