在导航抽屉式处理片段backstack [英] Handling fragment backstack in Navigation drawer

查看:189
本文介绍了在导航抽屉式处理片段backstack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道有其他<一href=\"http://stackoverflow.com/questions/21093287/navigation-drawer-handling-the-back-button-to-go-to-$p$pvious-fragments\">questions这乍一看使这一看起来像一个重复的,但这些答案在我的情况下工作,

okay i know there are other questions that on first glance make this one look like a duplicate, but none of these answers work in my case,

我要的是后退按钮是如何工作的,我需要我选择的任何片段从我的抽屉式导航回到第一个片段时,后退按钮是$ P显示要像尊重一个主要活动的第一个片段$ pssed那么用户将退出由$ p $应用程序再次pssing它。

What i want is the first fragment displayed to be like a Main Activity in respect to how the back button works, i need whichever fragment i choose from my navigation drawer to go back to the first fragment when the back button is pressed then a user would quit the app by pressing it again.

所以,香港专业教育学院使用addToBackStack尝试,当我移动到另一个片段,如果我preSS后退按钮它回来给我的第一个片段(正是我想要的),但$ P $再次pssing后退按钮留下我一个白屏(我不知道这是由于使用的香港专业教育学院包括以下交易动画IM),这样来解决这个问题,我试图重写返回键和通话抛来完成();但是这会导致任何片段即时通讯在,而不是完成的要回第一个片段,香港专业教育学院试图从上述链接的解决方法和许多其他极少数,但无法找到一个像样的修复有什么建议?

So ive tried using addToBackStack and when i move to another fragment if i press the back button it comes back to my first fragment (exactly as i want) but pressing the back button again leaves me with a white screen (i wonder if this is due to the transaction animation im using which ive included below) so to get around this i tried overriding the back button and throwing in a call to finish(); but this causes whichever fragment im in to finish instead of going back to the first fragment, ive tried a handful of workarounds from the above mentioned link and many others but cannot find a decent fix any suggestions?

这是我的主要活动displayView

here is my Main Activity displayView

 private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;

    switch (position) {
        case 0:
            fragment = new FirstFragment();

            break;

        case 1:
            fragment = new glideFrag();
            break;
        case 2:
            fragment = new secondGlideFrag();
            break;
        case 3:
            fragment = new thirdGlideFrag();
            break;
        case 4:
            fragment = new forthGlideFrag();
            break;

        case 5:
            fragment = new lgFrag();
            break;
        case 6:
            fragment = new cyanFrag();
            break;
        case 7:
            fragment = new sonyFrag();
            break;
        case 8:
            fragment = new SecondFragment();
            break;

        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.enter,R.anim.exit,R.anim.pop_enter,R.anim.pop_exit);

        //fragmentManager.beginTransaction()
                fragmentTransaction.replace(R.id.frame_container, fragment).addToBackStack("first Fragment").commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }

我发现这个看起来像周围的好办法

i found this that looks like a great way around it

private boolean popNext = false;

  if(popNext){
        if(position == INITIAL_POSITION){
            onBackPressed();
            mDrawerLayout.closeDrawer(mDrawerList);
            popNext = false;
            return;
        }
        getSupportFragmentManager().popBackStackImmediate();
    }
    else{
        if(position == INITIAL_POSITION){
            mDrawerLayout.closeDrawer(mDrawerList);
            return;
        }
        popNext=true;
    }

但IM仍然相当新的Andr​​oid和林不知道如何设置INITIAL_POSITION来,我试过

but im still fairly new to android and im not sure what to set INITIAL_POSITION to, I tried

private static final INITIAL_POSITION = 0; 

但没有任何运气

推荐答案

在添加初始片段,必须的的把它添加到后退堆栈。
您只能做下一个人。当返回堆叠将是空的,活动将只完成。

When adding the initial fragment, you must not add it to the back stack. You must only do it for the next ones. When the back stack will be empty, the Activity will just finish.

编辑:这是问题的解释,所以你可以找出如何解决它:

Here is an explanation of the problem so you can figure out how to fix it:

每次片段的交易增加背部栈,则允许用户通过pressing后退按钮和活动将恢复到它在交易前的状态恢复它。如果在初始片段被添加到后退堆栈,那么当用户preSS回来,屏幕变为空白,因为有你添加的初始片段之前没有任何显示。这就是为什么它增加了第一个可见片段的活动初始片段交易不能被添加到后退堆栈。通常你初始化初始片段是这样的:

Each time you add a fragment transaction to the back stack, you allow the user to revert it by pressing the back button and the Activity will return to the state it was before the transaction. If the initial fragment is added to the back stack, then when the user press back, the screen becomes blank, because there was nothing displayed before you added the initial fragment. That's why the initial fragment transaction which adds the first visible fragment to your Activity must not be added to the back stack. Usually you initialize the initial fragment like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState == null) {
        Fragment fragment = new FirstFragment();
        getSupportFragmentManager().beginTransaction()
            .add(R.id.frame_container, fragment)
            .commit();
    }
}

这篇关于在导航抽屉式处理片段backstack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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