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".

Luca93
  • 91
  • 6
  • 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. – Cruceo May 27 '14 at 15:18

2 Answers2

2

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

Reference: Providing Proper Back Navigation

aromero
  • 25,681
  • 6
  • 57
  • 79
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! :)

Scott W
  • 9,742
  • 2
  • 38
  • 53
  • Do I have to put "addToBackStack(null)" to every fragment that I want on the backStack ? – Luca93 May 28 '14 at 08:04
  • 1
    No, not every fragment, just every `FragmentTransaction`. – Scott W May 28 '14 at 13:01
  • 1
    Based on your edited question above, you need to add `addToBackStack()` to your `onButtonInfoSelected` transaction to solve your problem. – Scott W May 28 '14 at 13:03
  • I solved adding `addToBackStack` to "FragmentInfo" and "FragmentCams" only, NOT "FragmentPlaces". – Luca93 May 28 '14 at 15:14