Back to the main Fragment from a button

Asked

Viewed 65 times

0

Hi, I have an app that uses NavigationDrawer, I have a fragment that has a AlertDialog, and in it I have two send and cancel buttons, no cancel wish back to the desired main Fragment.

I managed to do it in a way that he’s coming back only the title on Toolbar is the old one still. Then I have to go back again so he can stay at the Ragment.

Man OnBackPressed of mainActivity:

public void onBackPressed() {

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        //retornar o drawer
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);

            // retorna todos os fragments que estão em backStack
        } else if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
            getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            toolbar.setTitle("Expresso1002");
        } else {
            super.onBackPressed();
        }

    }

And my exit from the cancel button:

btnCancelar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.content_principal, new principal());
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });

1 answer

0


Place toolbar.setTitle("Expresso1002"); onCreateView of the splinter leading() and every time this fragment is started, it will automatically change the title of your Toolbar.

  • I resolved so: getActivity().setTitle("Expresso 1002"); , Only when he comes back he opens another identity on top.

  • Note your transition between fragments. That is, note if you are using fragmentTransaction.add or fragmentTransaction.repleace. I noticed that you use Navigationdrawer for your transitions. So I recommend using whenever possible fragmentTransaction.repleace

  • I managed to settle by putting: getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); below the fragmentTransaction.commit();, vlw bro.

Browser other questions tagged

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