Fragment addToBackStack() and popBackStackImmediate() not working

前端 未结 6 1999
清酒与你
清酒与你 2020-12-08 20:00

I am currently building an application for Android (14 <= SDK <= 21) by using one ActionBarActivity and more Fragments, such as ListFra

相关标签:
6条回答
  • 2020-12-08 20:13

    Try this (Note add not replace for fragmentA, and addToBackStack() for fragmentB)

    StopItemFragment list = StopItemFragment.newInstance(null); //A - extends ListFragment
            fragmentManager.beginTransaction()
                .add(R.id.content_frame, list)
                .commit();
    

    and

    StopFragment fragment = StopFragment.newInstance(null, null); //B - extends Fragment
       ...
       fragmentManager.beginTransaction()
            .replace(R.id.content_frame, fragment)
            .addToBackStack("FragmentB")
            .commit();
    
    0 讨论(0)
  • 2020-12-08 20:13

    I had to call beginTransaction() and commit() of FragmentManager manually.
    Solved by overriding onBackPressed():

    @Override
    public void onBackPressed() {
        ...
        if (fragmentManager.getBackStackEntryCount() > 1){
            fragmentManager.popBackStackImmediate();
            fragmentManager.beginTransaction().commit();
        } else {
            //handle finish
            finish(); // Closes app
        }
    }
    
    0 讨论(0)
  • 2020-12-08 20:22

    For those, who are still looking for solution.

    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();
        }
    }
    

    There is no onBackPressed() method in fragment, and this method is just for the activity. So,when we press the back key, the default behaviour of activity is shown, which is

    you will either go to previous activity(if there is any) or the app will exit.

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

    0 讨论(0)
  • 2020-12-08 20:30

    If you are Struggling with addToBackStack() & popBackStack() then simply use

    FragmentTransaction ft =getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.content_frame, new HomeFragment(), "Home");
    ft.commit();`
    

    In your Activity In OnBackPressed() find out fargment by tag and then do your stuff

    Fragment home = getSupportFragmentManager().findFragmentByTag("Home");
    
    if (home instanceof HomeFragment && home.isVisible()) {
        // do you stuff
    }
    

    For more Information https://github.com/DattaHujare/NavigationDrawer I never use addToBackStack() for handling fragment.

    0 讨论(0)
  • 2020-12-08 20:30

    First of all, I wanted to say a big thank you to the highlighted [Nick]'s answer (Low reputation can't comment underneath), for helping me know there WAS actually something that didn't get mentioned in the Android Documentations.

    Second, it's 2019 now, and after stumbling for a similar way in Androidx's Preferences, I couldn't find anything for a long time, until I was both checking this question and an official sample code in Kotlin, which this question led to a clue and the sample code's way actually was the solution in my case, works like a charm with a very tiny piece of overwriting, hope it helps as well for those who are looking for an official solution

    @Override
    public boolean onSupportNavigateUp() {
    
        if (getSupportFragmentManager().popBackStackImmediate()){
            return true;
        }
    
        return super.onSupportNavigateUp();
    }
    

    Aforementioned code in Java (Screenshot)

    P.S. My first answer EVER on stacksoverflow, after more than of at least 5 years of using it ^^

    0 讨论(0)
  • 2020-12-08 20:39

    the reason why the popBackStack() and popBackStackImmediatly() is due to the fact that you didnt add that fragment (that you want to pop) to the backStack. In order to do this you have to make a call to addToBackStack() at the moment of making the transaction to add/replace your fragment.

    0 讨论(0)
提交回复
热议问题