Manage back button between Ragments

Asked

Viewed 3,881 times

1

I’m working on an app for the college where I’m using Fragments for all my screens, for reuse purposes. The problem is that I cannot control the behavior of the back button between the Fragments.

Inside a Fragment, I call another Fragment, but when I press the back button, the first Fragment does not return, but the Activity is terminated. I read that I could add Fragment to the stack that manages the return, but that doesn’t seem possible inside Fragment to Fragment.

The code I searched is this:

FragmentTransaction mFragmentTransaction = getFragmentManager()
            .beginTransaction();
....
mFragmentTransaction.addToBackStack(null);

To remove Fragment from stack:

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() == 0) {
        this.finish();
    } else {
        getFragmentManager().popBackStack();
    }
}

I withdrew that reply: https://stackoverflow.com/a/21635594, where the author made explicit that it works between activities. In my case, that I am calling a Fragment inside another Fragment, what would be the most indicated option? I believe that the behavior that I want, briefly, is this:

Activity 1 loads Fragment 1 automatically. Fragment 1 can call Fragment 2 at a given time, when the Fragment 2 is closed (either by button input of the proper Fragment [ok/cancel] as much as per back button of the device) it must return to Fragment 1.

  • Using this backstack alternative within Fragment 1 is not working? Do you see any errors? At first I see no problem in this solution... Is there any impediment to not delegate this replacement of fragments to the Activity? )?

  • Apparently the bugs corrected themselves. In what I posted the question, it worked. I will post my solution soon.

1 answer

4


Apparently the problem solved itself, but then I understood how the FragmentManager, solving the problem as follows:

Inside Fragment 1, I call Fragment 2, adding it to the stack:

    FragmentTransaction transaction = getFragmentManager()
            .beginTransaction();
    Fragment newUsr = new NewUserFragment();
    // essa linha é responsável por adicionar o fragment ao stack
    transaction.addToBackStack(null);
    transaction.replace(R.id.container_login, newUsr);
    transaction.commit();

In open Fragment, I can get back to the first one using a button in Actionbar with the following code:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id) {
        case R.id.menu_newuser_cancel:
            // essa linha é responsável por fechar o fragment,
            // simulando um toque do botão voltar, ao mesmo tempo em
            // que o remove do stack
            getActivity().getFragmentManager().popBackStack();
            break;
        }

With this, I was able to transition between Fragments from Fragments without problems with overlapping Fragments. It is worth adding here that in some cases the items of ActionBar were being duplicated, I resolved this by adding menu.clear(); within the method onCreateOptionsMenu:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.menu_newuser, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

With this, I was able to make the transition between Fragments efficiently and smoothly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.