3

I need to display a Fragment with 2 fragments before it being added into back stack. However addToBackStack method belongs to FragmentTransaction so I cannot add all three of them in the single FragmentTransaction cause all three of them will be removed by back button click. But if I use three different FragmentTransations then until the third fragment becomes visible two previous ones become visible to the user too.

Is there a way I can add three Fragments into back stack without making first two of them visible during transaction?

Lingviston
  • 5,479
  • 5
  • 35
  • 67

2 Answers2

0

I am not sure you can do that using the native API. Nonetheless you can implement your own stack using the Queue Interface

suns9
  • 875
  • 2
  • 8
  • 17
  • Yeah, but unfortunately I need to use the native API because I need my code to be compatible with older version. – Lingviston Mar 22 '16 at 11:49
  • No links between using Queue Interface (Java Context) and old android versions (Android Context). I mean this is not because you implement your own stack (using Queue Interface) that your code will not be compatible with the oldest android versions – suns9 Mar 22 '16 at 14:21
  • My code is an Android library. It'll be incompatible with some of my library users' integrations if I switch to custom queue. – Lingviston Mar 22 '16 at 17:22
0

add an OnBackStackChangedListener to fragmentmanager , then when the last backstack entry is fragment 2 popBackStack two times like this :

 0 FragmentHome ( back_stack_name : "fragment_home" ) 
 1 Fragment1( back_stack_name : "fragment_1" ) 
 2 Fragment2 ( back_stack_name : "fragment_2" ) 
 3 Fragment3 ( back_stack_name : "fragment_3" ) 

code :

note that you should add OnBackStackChangedListener in fragment3 and then after poping backstack remove it from fragmentmanager

    // add in fragment3
        final FragmentManager fragment_manager = getActivity().getSupportFragmentManager();
            fragment_manager
                    .addOnBackStackChangedListener(new OnBackStackChangedListener() {

                        @Override
                        public void onBackStackChanged() {
                            if (fragment_manager.getBackStackEntryCount() > 0) {
                                String last_fragment_name = getLastBackStackFragmentName(fragment_manager);
                                if (last_fragment_name.equals("fragment_2")) {
                                      fragment_manager.removeOnBackStackChangedListener(this);
                                     fragment_manager.popBackStack();
                                     fragment_manager.popBackStack();

                                }
                            }
                        }
                    });

        private String getLastBackStackFragmentName(FragmentManager fragment_manager ) {
            int back_stack_count =fragment_manager.getBackStackEntryCount();
            String last_fragment_name = "";
            if (back_stack_count>0) {
                last_fragment_name = fragment_manager.getBackStackEntryAt(
                        back_stack_count).getName();
            }
            return last_fragment_name;
        }
Mohsen fallahi
  • 915
  • 1
  • 10
  • 27
  • If I understand you right then after clicking back button on Fragment3 I'll be navigated to HomeFragment. I need an opposite: after clicking back button on Fragment3 I should be navigated to Fragment2 but when I'm adding the Fragment3 into fragment manager I should also add Fragment1 and Fragment2 but in a way user can't notice it. – Lingviston Mar 22 '16 at 13:01
  • 1
    @Lingviston hide those two fragments after they are added to backstack and then show them after fragment 3 is shown http://stackoverflow.com/questions/14347588/show-hide-fragment-in-android – Mohsen fallahi Mar 22 '16 at 15:02
  • funny thing is that I already ended up doing it this way:D But thanks anyway! – Lingviston Mar 22 '16 at 17:22