如果有一个片段不在backstack中,则在弹出堆栈之前添加的片段没有再出现

时间:2014-01-21 07:13:50

标签: android android-fragments

第1步:点击addNewFragmentBtn添加新片段[A]并将其添加到backstack

step2:添加另一个不在backstack中的片段[B]

第3步:点击addNewFragmentBtn添加新片段[C]并将其添加到Backstack

步骤4:单击返回btn返回上一个片段。

整个进度应为A->B->C-B->A,如果我在步骤4之后继续单击后退按钮,则后备栈条目的计数减少但片段A不可重新访问。 我怎样才能附上片段A?

        public class Main extends FragmentActivity {
        private int i=1;
        private int j=1;
        public FragmentTransaction fragmentTransaction;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            final View addNewFragmentBtn = findViewById(R.id.add_new_fragment);
            final View backBtn = findViewById(R.id.back);
            final View addNewFragmentNotInStackBtn = findViewById(R.id.add_new_fragment_not_in_stack);

            addNewFragmentBtn.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fragmentTransaction = getSupportFragmentManager().beginTransaction();
                            FragmentSample fragment = new FragmentSample();
                            Bundle args = new Bundle();
                            args.putString("key1", "fragment1-" + i + "is in backstack");
                            fragment.setArguments(args);
                            fragmentTransaction.add(R.id.content, fragment);
                            fragmentTransaction.addToBackStack(null);
                            fragmentTransaction.commit();
                            i++;
                        }
                    }
            );
            addNewFragmentNotInStackBtn.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fragmentTransaction = getSupportFragmentManager().beginTransaction();
                            FragmentSample fragment = new FragmentSample();
                            Bundle args = new Bundle();
                            args.putString("key2", "fragment2-" + j + " is not in backstack");
                            fragment.setArguments(args);
                            fragmentTransaction.add(R.id.content, fragment);
    //                        fragmentTransaction.addToBackStack(null);
                            fragmentTransaction.commit();
                            j++;
                        }
                    }
            );
            backBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
                    if (backStackEntryCount > 0) {
                        getSupportFragmentManager().popBackStack();
                    } else finish();
                }
            });

        }
    }

1 个答案:

答案 0 :(得分:0)

这是我的解决方法: 我用标签对它们进行分类:你想要放在后面堆栈中的一种片段给它们一个标签“A_STACK”,其他你不想恢复的标签,添加一个标签“B_STACK”。

BackBtn.onClick方法中的

    public class Main extends FragmentActivity {
        private int i = 1;
        private int j = 1;
        public FragmentTransaction fragmentTransaction;
        private final static String NO_HISTORY_FRAGMENT = "NO_HISTORY_FRAGMENT";
        private final static String KEEP_HISTORY_FRAGMENT = "KEEP_HISTORY_FRAGMENT";
        public String KEEP_HISTORY_FRAGMENT_STACK = "KEEP_HISTORY_FRAGMENT_STACK";
        public String NO_HISTORY_FRAGMENT_STACK = "NO_HISTORY_FRAGMENT_STACK";

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


            final View addNewFragmentBtn = findViewById(R.id.add_new_fragment);
            final View backBtn = findViewById(R.id.back);
            final View addNewFragmentNotInStackBtn = findViewById(R.id.add_new_fragment_not_in_stack);

            addNewFragmentBtn.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fragmentTransaction = getSupportFragmentManager().beginTransaction();


                            FragmentSample fragment = new FragmentSample();
                            Bundle args = new Bundle();
                            args.putString("key1", "fragment1-" + i + "is in backstack");
                            fragment.setArguments(args);
                            fragmentTransaction.add(R.id.content, fragment, KEEP_HISTORY_FRAGMENT);
                            fragmentTransaction.addToBackStack(KEEP_HISTORY_FRAGMENT_STACK);
                            fragmentTransaction.commit();
                            i++;
                        }
                    }
            );
            addNewFragmentNotInStackBtn.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fragmentTransaction = getSupportFragmentManager().beginTransaction();
                            FragmentSample fragment = new FragmentSample();
                            Bundle args = new Bundle();
                            args.putString("key2", "fragment2-" + j + " is not in backstack");
                            fragment.setArguments(args);
                            fragmentTransaction.add(R.id.content, fragment, NO_HISTORY_FRAGMENT);
                            fragmentTransaction.addToBackStack(NO_HISTORY_FRAGMENT_STACK);
                            fragmentTransaction.commit();
                            j++;
                        }
                    }
            );
            backBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
                    if(backStackEntryCount>0){
                        FragmentSample currentFragment = (FragmentSample) getSupportFragmentManager().findFragmentByTag(NO_HISTORY_FRAGMENT);
                        if(currentFragment!=null){
                            getSupportFragmentManager().popBackStack(NO_HISTORY_FRAGMENT_STACK, 1);//clear the not necessary fragment stack
                            getSupportFragmentManager().popBackStack(KEEP_HISTORY_FRAGMENT_STACK, 0);//switch to the usefull stack
                        }else {
                            getSupportFragmentManager().popBackStack();
                        }
                    }else finish();
                }
            });

        }
    }