0

I have a small layout in my activity that I add Fragments to based on the User navigating through the app.

Assuming the user navigates thusly:

Activity -> Fragment A -> Fragment B -> Fragment C -> Button Click

I would like to be able to to hide the Fragments and show the blank Activity again.

This is how I'm adding the Fragments to the activity:

protected void addFragment(Fragment fragment)
    {
        getSupportFragmentManager().beginTransaction().replace(R.id.secondary_fragment, fragment).addToBackStack(fragment.getTitle()).commit();
    }

To clear all the Fragments, I use:

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

However, is there a way to clear the fragments in a way that if the user presses back, they would be able to go back to Fragment C (as opposed to exiting the App)?

StackOverflowed
  • 5,854
  • 9
  • 55
  • 119

2 Answers2

0

Maybe instead of pop all the backStack, you just get the fragment view by id and setVisibility to invisible?

Surely
  • 1,649
  • 16
  • 23
  • I thought about that.. . I guess to enable the "back" button I'd just setVisibility back to Visible – StackOverflowed May 01 '15 at 16:45
  • @StackOverflowed yup, override the onBackPressed method, check if the view is invisible, if yes, make it visible, otherwise do super.onBackPressed. – Surely May 01 '15 at 16:51
0

Try starting a new instance of your Activity with a clear stack on the button press (if I'm correct in assuming this comes after C as you described). This way the First Activity instance will still have up to Fragment C and the Second Activity instance will be whatever you like (Fragment A > Fragment D > Fragment F). And you won't need to pop/clear any back stack for any Activity.

HTHs

petey
  • 16,914
  • 6
  • 65
  • 97
  • That is actually a useful way of doing it. I'm trying to avoid multiple Activities however, – StackOverflowed May 01 '15 at 17:25
  • It can totally be used this way if needed. Give it a try as I believe it will amount to less fragment management that you have to do in your app. – petey May 01 '15 at 17:59