Replace Fragment with another on back button

后端 未结 3 813
北恋
北恋 2021-01-20 21:36

I\'m trying to override the Back Button because it\'s closing my app when I push on, I have different Fragments:

  • Fragment A: Index (When I press back button, i
3条回答
  •  遇见更好的自我
    2021-01-20 22:01

    This is what I use when navigating between fragments:

    MainActivity.java:

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

    EDIT the second: Please note that you ONLY want to call super.onBackPressed() if you haven't already handled it (by, for example, popping the fragment manager's backstack).

    For that to work, you have to add new fragments to your FragmentManager's backstack (addToBackStack()). For example (also in MainActivity.java):

    private void displayView(int position) {
        Fragment fragment = ...; // YOUR CODE HERE
        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment);
    
            // ADD THIS LINE
            fragmentTransaction.addToBackStack("name"); // name can be null
    
            fragmentTransaction.commit();
            // libelle du toolbar
            TextView titlet;
            titlet = (TextView) findViewById(R.id.main_toolbar_title);
            titlet.setText(title);
            titlet.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/GothamBook.ttf"));
        }
    }
    

    EDIT the third (7/28): In your onCreate(Bundle) method, you do your very first fragment transaction by calling your displayView(int) method. displayView(int) always adds its fragment transactions to the backstack. This is not what you want. The very first fragment transaction should use fragmentTransaction.**add**(int, Fragment) and should not call addToBackStack(String). Every transaction after the first should call fragmentTransaction.**replace**(int, Fragment) and should call addToBackStack(String). The reason for this is that you first transaction is basically "adding" a fragment (your UI) to an empty container (it is not "replacing" another fragment). When this transaction is on the backstack, that means that the empty-container state is also on the backstack. So when you pop that last transaction, it displays a blank UI.

    EDIT the first: When you call addToBackStack(String name) on a FragmentTransaction object (which you obtain by calling getFragmentManager().beginTransaction()), then you are adding a FragmentTransaction to your FragmentManagers 'backstack'. What my code does is check the size of the backstack by calling getFragmentManager.getBackStackEntryCount(). If that number is greater than zero, then we know we have FragmentTransactions on the backstack. In such a case, you can call getFragmentManager.popBackStack(), which will pop the last transaction off the backstack--in other words, returning your app to the last Fragment that was on display.

    If the backstack entry county equals 0, then that means you're at your Fragment A, and you should instead call super.onBackPressed(), and this will cause the app to exit.

提交回复
热议问题