Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android back button navigate to specific fragment

I have the navigation drawer menu structure presently setup to allow for going back to the previous fragmnet, with addToBackStack, when selecting the back button as:

    if (menuItem.getItemId() == R.id.nav_item_messages) {
            FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
            xfragmentTransaction.replace(R.id.containerView, new MessageTabFragment()).addToBackStack("MainActivity").commit();
        }

However, I wish to allow for the back button to always send the user back to a specific framgment, not simply the previous one.

How can this be accomplished?

amended

I attempted adding the code:

int homeFragmentIdentifier = -1;

...

            if (menuItem.getItemId() == R.id.nav_item_messages) {
                FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                if(homeFragmentIdentifier == -1) {
                    homeFragmentIdentifier = xfragmentTransaction.replace(R.id.containerView, new MessageTabFragment()).commit();
                } else {
                    xfragmentTransaction.replace(R.id.containerView, new MessageTabFragment()).commit();
                }

            }

...

@Override
public void onBackPressed() {
    super.onBackPressed();

    getFragmentManager().popBackStack(homeFragmentIdentifier, 0);

}

But it simply calls the error:

01-15 19:31:55.368 18410-18410/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.android.feastappv2, PID: 18410
                                                   java.lang.IllegalArgumentException: Bad id: -1
                                                       at android.app.FragmentManagerImpl.popBackStack(FragmentManager.java:526)
                                                       at com.example.android.feastappv2.feastapp.MainActivity.onBackPressed(MainActivity.java:143)
                                                       at android.app.Activity.onKeyUp(Activity.java:2667)
                                                       at android.view.KeyEvent.dispatch(KeyEvent.java:3214)
                                                       at android.app.Activity.dispatchKeyEvent(Activity.java:2922)
like image 295
Sauron Avatar asked Jan 15 '16 03:01

Sauron


1 Answers

Take a look at this: FragmentManager.popBackStack(int, int)

When you commit your fragment transaction, the method returns an identifier for that transaction. Save that int identifier:

FragmentTransaction xfragmentTransaction = getFragmentManager().beingTransaction();
xfragmentTransaction.replace(R.id.containerView, new MessageTabFragment());
xfragmentTransaction.addToBackStack("MainActivity");
int homeFragmentIdentifier = transaction.commit();

then, in your onBackPressed() method, you can add either of the following lines:

getFragmentManager().popBackStack(homeFragmentIdentifier, 0); // Exclusive

or

getFragmentManager().popBackStack("MainActivity", 0); // Exclusive

This will pop the back stack all the way back to the supplied identifier. Alternatively, if you want to include the homeFragmentIdentifier in the "pop"ing, instead of 0 use FragmentManager.POP_BACK_STACK_INCLUSIVE for the 2nd parameter. You still must call FragmentTransaction.addToBackStack(). According to the docs:

public abstract int commit () ... Returns the identifier of this transaction's back stack entry, if addToBackStack(String) had been called. Otherwise, returns a negative number.

Also, if you set the tag of the fragment when adding it to the backstack, for example:

transaction.add(yourFragment);
transaction.addToBackStack("[YourFragmentTransactionTag]");

You can use the alternate method: FragmentManager.popBackStack(String, int) by supplying the tag you used when adding the transaction to the backstack.

getFragmentManager().popBackStack("[YourFragmentTransactionTag]", 0); 
like image 148
Andrew Senner Avatar answered Oct 31 '22 14:10

Andrew Senner