popBackStack() after addToBackStack does not work

后端 未结 6 1262
梦毁少年i
梦毁少年i 2021-02-11 20:43

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 21:09

    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.

提交回复
热议问题