popBackStack() after addToBackStack does not work

后端 未结 6 1686
野趣味
野趣味 2021-02-11 20:29

My project contains two fragment :

  • FragmentA : the fragment loaded by default when the app starts
  • FragmentB : replace the fragmentA when a c
6条回答
  •  失恋的感觉
    2021-02-11 20:59

    If you are Struggling with addToBackStack() and 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.

提交回复
热议问题