片段addToBackStack()和popBackStackImmediate()不工作 [英] Fragment addToBackStack() and popBackStackImmediate() not working

查看:923
本文介绍了片段addToBackStack()和popBackStackImmediate()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建设为Android应用程序(14< = SDK< = 21),通过使用一个 ActionBarActivity 片段,如 ListFragment MapFragment ,这是在一个交换的FrameLayout 视图。

I am currently building an application for Android (14 <= SDK <= 21) by using one ActionBarActivity and more Fragments, such as ListFragment and MapFragment, which are swapped within a single FrameLayout view.

该ActionBarActivity自动替换/提交片段A.然后,当用户点击一个按钮,主办活动替换/提交一个新的不同的片段B.我的目标是让用户反悔碎片A只要她presses后退按钮。

The ActionBarActivity automatically replace/commit fragment A. Then, when the user tap a button, the hosting Activity replace/commit a new different fragment B. My goal is to let the user go back on fragment A as soon as she presses the back button.

现在有些code。

MainActivity

MainActivity

public class MainActivity extends ActionBarActivity implements StopFragment.OnFragmentInteractionListener,
    StopItemFragment.OnFragmentInteractionListener {
...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentManager = getFragmentManager();
        fragmentManager.enableDebugLogging(true);
        ...
        if (fragmentManager.findFragmentById(R.id.content_frame) == null) {
            StopItemFragment list = StopItemFragment.newInstance(null); //A - extends ListFragment
            fragmentManager.beginTransaction()
                .replace(R.id.content_frame, list)
                .addToBackStack(null)
                .commit();
        }
        ...

        @Override
        public void onFragmentInteraction(String id) {
        selectItem(Integer.parseInt(id));
        }


       private void selectItem(int position) {
       StopFragment fragment = StopFragment.newInstance(null, null); //B - extends Fragment
       ...
       fragmentManager.beginTransaction()
            .replace(R.id.content_frame, fragment)
            .commit();

       ...
       }
}


问题


Problem

即使 addToBackStack()被调用,当我B片段上,我不能回去片段A. MainActivity直接关闭。 然而,我想我自己没有运气来管理回堆栈。我可以看到,该片段是在栈上,但如果我叫 popBackStackImmediate(),碎片A被弹出,并且不执行片段交易。 (第一回preSS没有发生,第二个活动关闭)

Even if addToBackStack() is called, when I am on fragment B, I am not able to go back to fragment A. MainActivity is directly closed. Yet I tried to manage the back stack by myself with no luck. I can see that the fragment is on the stack but if I call popBackStackImmediate(), fragment A is popped out and the fragment transaction is not performed. (first back press nothing happen, second activity closed)

我还附上了FragmentManager logcat的:
http://pastebin.com/hFLHprL8

I attach also the FragmentManager logcat:
http://pastebin.com/hFLHprL8

推荐答案

对于那些,谁仍在寻找解决方案。

For those, who are still looking for solution.

在主要活动类(托管片段)刚刚覆盖onBack pressed()。

In the main activity class (which is hosting the fragments)just override onBackPressed().

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0 ){
        getFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}

没有onBack pressed()方法在片段,而这种方法只是对活动。所以,当我们preSS返回键,活动的缺省行为表示,这是 - 你要么去previous活动(如果有的话),或者应用程序将退出

There is no onBackPressed() method in fragment, and this method is just for the activity. So,when we press the back key, the default behavior of activity is shown, which is -- "you will either go to previous activity(if there is any) or the app will exit".

现在我们需要重写此方法来告诉我们preSS返回键的活动,如果在backstack任何片段,弹出出来(这是当addToBackStack()进入画面)。 ....否则按照默认的行为。

Now we need to override this method to tell the activity that when we press the back key, if there are any fragments in backstack, pop them out(and this is when the addToBackStack() comes into picture)..... otherwise follow the default behavior.

这篇关于片段addToBackStack()和popBackStackImmediate()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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