Manage button back between fragments

1

I'm working on an application for college in which I'm using fragments for all my screens for reuse issues. The problem is that I can not control the behavior of the back button between fragments.

Within a fragment, I call another fragment, but when I hit the back button, the first fragment does not return, but rather the activity is terminated. I read that I could add the fragment on the stack that manages the back, but that does not seem to be possible within fragment to fragment.

The code I searched for is this:

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

To remove the stack fragment:

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

I have removed this response: link , where the author has explicitly stated that it works between activities. In my case, what I'm calling a fragment within another fragment, what would be the most appropriate option? I think the behavior I want, briefly, is this:

  

Activity 1 loads Fragment 1 automatically. Fragment 1 can   Fragment 2 at any given time, when the   Fragment 2 is closed (either by the button's own input   [ok / cancel] as much as by push button   should return to Fragment 1.

    
asked by anonymous 22.05.2014 / 19:28

1 answer

4

Apparently the problem solved itself, but then I understood how FragmentManager worked, 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 the open fragment, I can go back to the first one by 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's worth adding here that, in some cases, ActionBar items were being duplicated, I solved this by adding menu.clear() ; within the onCreateOptionsMenu method:

@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 transpose the fragments efficiently and without problems.

    
22.05.2014 / 22:06