How to return to my Actionbaractivity?

Asked

Viewed 143 times

0

For example, for example... I have an application that has a main Actionbar and I "Seto" an Activity that has a drawerLayout (which has a side menu)... In each menu option -case selected- I replace the Framelayout added to my Drawerlayout with a Fragment... As I set it to when I press the title of my Actionbar or the icon on the left side, it returns to the Activity that the application started -which in case is not a Fragment-?

  • Let me get this straight: you have a main Activity with a Drawerlayout. These menu options are Fragments (as it should be) and if it is not in the main Fragment, you want when touching the title it goes back to the main one? Or have some code to better illustrate your question?

1 answer

1


Your Activity is the set of Drawer, Actionbar and any Fragments you add to it. Imagine Activity as a 'box' on you put 'blocks', which are the Fragments, so every time you select a new item in Drawer it changes the 'block' of your 'box'.

If you started Activity it must contain a 'main' Fragment, 'home', 'main', as you prefer to call, to be added to your Framelayout. After navigating the application through the menu your Framelayout will be replaced by other Fragments (Fragmentmanager replace command). If you use the method 'addToBackStack("name")' the Fragmentmanager of your Activity will create a stack of Fragments that will be popped if you use the method 'onBackPressed()'.

In case you want, when clicking on some button you see again your 'Home' just replace for your 'home' fragment. And if you were stacking Fragments with addToBackStack("name")' you can clear the stack using:

getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

And then replace to your main fragment:

getSupportFragmentManager().beginTransaction() .replace(R.id.FrameLayout, FragmentMain.newInstance(), "fragmentMain") .commit();

  • Thank you @Rafael... He had not even called me that even though I pressed the button to go back home, when I pressed the back of his own device he would go back to the Fragments that were open!!

Browser other questions tagged

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