0

My app use 5 fragment, like this:

[1]through[onClick on actionBar defined in activity]->[2]->[3]->[4]->[5]

Each fragment is added to the back stack so I can go back while pressing the back button. However, I would like to return to the first fragment when pressing the back button on the 5th fragment, like this:

[1]<-[2]<-[3]<-[4]   [1]<-[5]

I tried to make it this way:

fragmentTransaction.addToBackStack("firstfragmenttag");

When adding the 5th fragment on the 4th one, but when I press the back button it still send me back to the 4th instead of the 1st! Is it a simple way do do that programmatically? Thanks in advance.

Virthuss
  • 3,142
  • 1
  • 21
  • 39
  • Check this http://goo.gl/AXku8c – Muhammad Babar Oct 06 '15 at 09:54
  • Got it, I'm trying. And by the way, to the guy who downvoted me, explain why please... – Virthuss Oct 06 '15 at 10:10
  • @MuhammadBabar This is saddly not working – Virthuss Oct 06 '15 at 10:40
  • Did you tried with `POP_BACK_STACK_INCLUSIVE` as flag? – Muhammad Babar Oct 06 '15 at 10:45
  • yes. But maybe I was not clear... Its the action bar, defined in the Activity ( but the activity is totally empty except that ), which launches F2. The activity only launchs the fragment F1 containing the first view the one I need to reach, but since the F2 is called on a click on the action bar, I guess the popbackstack will never find the fragment I need to reach.. – Virthuss Oct 06 '15 at 10:52

2 Answers2

1

In your case, you just need to add first fragment in stack.

No need to add others to stack This could be achieved in following way:

Fragment F1 = new <Fragment Name>();
fragmenttransaction.add(R.id.content,F1).commit();

And for others like F2,F3,F4 & F5 you could use

Fragment F2 = new <Fragment Name>();
fragmenttransaction.replace(R.id.content,F2).addToBackstack(null).commit();

And then you need to override OnBackpressed [inside Activity] like below:

  @Override
public void onBackPressed() {
    if(getSupportFragmentManager().getBackStackEntryCount() >0) {
        getSupportFragmentManager().popBackStack();
    }
    else{
        super.onBackPressed();
    }
}

Hope it helps!

PunitD
  • 2,293
  • 1
  • 20
  • 29
  • I think I can use this one. Is there a way to know from which fragment come the call and can I add some parameters? I have others fragment accessing the 5th fragment and in this case, I need to keep the normal behaviour. Only the 4th one has to remain out of the backstack in this case – Virthuss Oct 06 '15 at 08:21
  • will your sequence always go like [F1]->[F2]->[F3]->[F4]->[F5] or would it be random ? And if it goes in above sequence from F1 to F5 you want rest all to remain in stack while moving out F4 out of stack? Do i understand that correctly? – PunitD Oct 06 '15 at 08:25
  • I have others fragment which can access [F5] as well. The only thing is, when I access [F5] from [F4], I need to go on [F1] when the user press the back button, not in [F4]. For the rest, everything is good as it is. – Virthuss Oct 06 '15 at 08:27
  • Then in your case i think you need to write if and else where you check if F4 fragment is visible or not like: `if(F4.isVisible() && !F5.isVisible()){'fragmenttransaction.replace(R.id.content,F5).addToBackstack(null).commit();} else{ //your normal way }` – PunitD Oct 06 '15 at 08:33
  • I'musing a bundle parameter for that, I will try this. But how can I specifiy that the back stack for fragment 5 leads to fragment 1 and not to the 4th? – Virthuss Oct 06 '15 at 08:35
  • By making sure you don't add F2,F3 and F4 to backstack using the code provided in answer – PunitD Oct 06 '15 at 08:36
  • But if I do so, I cannot access F3 from F4, F2 from F3... I need to do so! I just dont want to access F4 from F5 ( by accessing F1 instead ) – Virthuss Oct 06 '15 at 08:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91458/discussion-between-droidwala-and-virthuss). – PunitD Oct 06 '15 at 08:48
0

you can actually get the backstack count in onBackPressed and check count , if it is 5 then perform popfrombackstack 4 times to get back to 1 fragment

Sangeet Suresh
  • 2,527
  • 1
  • 18
  • 19
  • The thing is my fragment can also be accessed by others ( not just by [4] ) so saddly this solution in my case doesnt work... – Virthuss Oct 06 '15 at 10:11