Android的碎片后退按钮覆盖其他片段,并保持活跃 [英] Android Fragment Back Button overlays other fragment and keeps active

查看:191
本文介绍了Android的碎片后退按钮覆盖其他片段,并保持活跃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有3个片段。

应用程序启动与[1],用户只能定位到[2],然后选择性[3]。

由于[3]是在内心深处,我希望[3]回去[1]直接。

目前我叫[2]使用addToBackStack(空)。因为我不叫addToBackStack上[3]我以为它会回到[1]。

会发生什么事是,[3]返回[1],但现在这两个片段显示重叠。片段[3]不调用的onPause()。调用片段[2]将再次显示[2]对别人的顶部,而不是清除屏幕。

导航来回会崩溃的应用程序。有时,击中背部按钮时,打开一个新的片段,有时。始终以碎片已添加的错误(这是后退按钮非常奇怪,但我检查也切换片段之前)。

任何想法可能会导致此奇怪的行为?使用addToBackStack或[3]也消除了该问题,但并没有解决我的要求。我呼吁在适当情况下所有超级方法。

信息:code样品在 http://beadsoft.de/android/FragmentTest下载。拉链

在2.x中,4.2.2相同的行为。使用ActionBarCompat。

code添加片段:

  FragmentManager FM = getActivity()getSupportFragmentManager()。
    片段片段= fm.findFragmentByTag(Fragment_xyz.FRAG_TAG);
    如果(片段== NULL)
        片段= Fragment_xyz.newInstance(int数据);
    fm.beginTransaction()取代(R.id.container,片段,Fragment_xyz.FRAG_TAG)
            .addToBackStack(空).commit()


解决方案

为什么不增加每一个片段的backstack?相反的:

  MyNewFragment F =新MyNewFragment();
。getSupportFragmentManager()调用BeginTransaction()取代(R.id.container,F).commit()。

尝试:

  MyNewFragment F =新MyNewFragment();
。getSupportFragmentManager()调用BeginTransaction()addToBackStack(空).replace(R.id.container,F).commit()。

In my App I have 3 fragments.

App Starts with [1], User can only navigate to [2] and then optionally to [3].

Since [3] is deep down, I want [3] to go back to [1] directly.

Currently I call [2] using addToBackStack(null). Since I do NOT call addToBackStack on [3] I assumed it will go back to [1].

What happens is, that [3] returns to [1], but now both fragments are displayed overlapping. Fragment [3] does not call onPause(). Calling fragment [2] again will display [2] on top of the others, not clearing the screen.

Navigating back and forth will crash the app. Sometimes on opening a new fragment, sometimes when hitting the back button. Always with "Fragment already added" error (which is extremely odd for the back button, but I check that also before switching fragments).

Any ideas what might cause this odd behavior? Using addToBackStack or [3] also eliminates the problem, but does not solve my requirement. I do call all super methods where appropriate.

Info: Code Sample for Download at http://beadsoft.de/android/FragmentTest.zip

Same behavior on 2.x, 4.2.2. Using ActionBarCompat.

Code for adding fragment:

    FragmentManager fm = getActivity().getSupportFragmentManager();
    Fragment fragment = fm.findFragmentByTag(Fragment_xyz.FRAG_TAG);
    if (fragment == null)
        fragment = Fragment_xyz.newInstance(int data);
    fm.beginTransaction().replace(R.id.container, fragment, Fragment_xyz.FRAG_TAG)
            .addToBackStack(null).commit()

解决方案

why not adding every fragments to the backstack? Instead of:

MyNewFragment f=new MyNewFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, f).commit();

Try:

MyNewFragment f=new MyNewFragment();
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.container, f).commit();

这篇关于Android的碎片后退按钮覆盖其他片段,并保持活跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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