Fragment pressing back button

后端 未结 16 2418
清歌不尽
清歌不尽 2020-11-29 20:18

I am now having an activity containing fragments

[1] , [2] , [3] , [4]

If pressing buttons , [3] , it can be redirected to [4]

I would like to imp

16条回答
  •  旧时难觅i
    2020-11-29 20:53

    i use a methode to change fragments it has thw following code

     getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit).replace(R.id.content_frame, mContent, mContent.getClass().getSimpleName()).addToBackStack(null)
                    .commit();
    

    and for the back button this .

    @Override
        public void onBackPressed() {
            // note: you can also use 'getSupportFragmentManager()'
            FragmentManager mgr = getSupportFragmentManager();
            if (mgr.getBackStackEntryCount() == 1) {
                // No backstack to pop, so calling super
                finish();
            } else {
                mgr.popBackStack();
            }
        }
    

    the important thing to note is i use 1 for checking getBackStackEntryCount this is because if you dont use it and use 0 user sees nothing for the last back button.

提交回复
热议问题