Android fragments navigation and backstack

后端 未结 1 1890
暖寄归人
暖寄归人 2021-01-05 03:35

I have a header bar (kinda like menu) and 4 fragments (MAIN, A, B, C) from which the MAIN should be \'main/root\' fragment for backstack.

Problem i have is when user

相关标签:
1条回答
  • 2021-01-05 04:01

    Your stack must contains 2 fragments at maximum

    Main is visible Main is onBackstack / AorBorC is visible. User click on back ==> Main is visible. User click on back ==> application end

    I suppose A / B / C are displayed in the same view so in this case,

    When user click on your Menu, you have to check if A/ B / C is currently displayed and replace it by the one selected by the user.

    private void displayFragment(Fragment pFragment) {
        Fragment fr = getSupportFragmentManager()
                .findFragmentById(R.id.main_fl_fragmentcontainer);        
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.main_fl_fragmentcontainer, pFragment);
        if (_CurrentFragment == _Frag_Main) {
            ft.addToBackStack(null);
        }
        ft.commit();    
        _CurrentFragment = pFragment;
    }
    

    can override OnBackPressed method of your activity.

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