Android - addToBackStack() doesn't work?

前端 未结 3 1349
旧时难觅i
旧时难觅i 2021-02-07 14:08

This is the function responsible for adding fragments to back stack:

public void populateContent(File f)
{

    ContentFragment cf = new ContentFragment(ctx, ac,         


        
3条回答
  •  后悔当初
    2021-02-07 14:21

    It seems that calling addToBackStack() on fragment transaction is not enough, we have to handle the popping up of the back stack upon Back button pressed by ourselves. I added this to my activity and it worked as expected:

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 0 ){
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }
    

提交回复
热议问题