0

My Application has one Activity with a layout (FrameLayout) for many fragments.

  • At the start of the Application it displays a list of Places (PlacesFragment).
  • When a Place is clicked, a Fragment that displays a list of cams (CamFragment) is added. Inside this fragment there is also a button "info".
  • When the user press the "info" button a new fragment (InfoPlaceFragment) that displays information about the place, is added.

Here is a graphical explanation: enter image description here

I want to be able to go back to "CamFragment(B)" from "InfoPlaceFragment(C)", and to go back to "PlacesFragment(A)" from "CamFragment(B)".

  • One Question:
    1) Is it possible to realize this or an alternative solution is better?

------- EDIT -------

SOLUTION:

In the "MainActivity.java":

onPlaceParse() // This method is called when the data from the server is received:

 // Here I create the "PlaceFragment" fManager = getSupportFragmentManager(); fragmentPlaces = new PlacesFragment(); fManager.beginTransaction() .replace(R.id.fragment_list_container, fragmentPlaces) .commit(); 

onResortSelected // This method is called when the resort is selected

 fragmentCams = new CamsFragment(); fManager.beginTransaction() .replace(R.id.fragment_list_container, fragmentCams) .addToBackStack(null) .commit(); 

onButtonInfoSelected // This method is called when the btn info is selected

 fragmentInfo = new InfoResortFragment(); fManager = getSupportFragmentManager(); fManager.beginTransaction() .replace(R.id.fragment_list_container, fragmentInfo, "Info") .addToBackStack(null) .commit(); 

When you press the back button, if you are in the "Info Fragment" it returns to "PlaceFragment" and if you are in the "CamsFragment" it returns to "PlacesFragment".

1
  • 1
    Is there something not working or are you asking if it's just possible? If your question is the latter, then yes, that's what the FragmentManager's backstack will allow you to do via addToBackStack() and popBackStack() transactions. Commented May 27, 2014 at 15:18

2 Answers 2

2

This is possible, you need to call addToBackStack(null) in the fragment transactions.

Reference: Providing Proper Back Navigation

Sign up to request clarification or add additional context in comments.

Comments

1

When you create your FragmentTransaction that will add/remove/replace (or whatever pattern you are using) the Fragments, just make sure to call addToBackStack(). Then, when the user presses the back button, the system will automatically cycle back through the entries added to the back stack, in reverse order from how they were originally added. No additional work is required on your part! :)

4 Comments

Do I have to put "addToBackStack(null)" to every fragment that I want on the backStack ?
No, not every fragment, just every FragmentTransaction.
Based on your edited question above, you need to add addToBackStack() to your onButtonInfoSelected transaction to solve your problem.
I solved adding addToBackStack to "FragmentInfo" and "FragmentCams" only, NOT "FragmentPlaces".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.