0

Please have a look on this pattern. Is it possible ???

Fragment A > Fragment B > Fragment C > Fragment D > Fragment E

index 0 > index 1 > index 2 > index 3 > index 4

Now what I want : >>>

Switch from Fragment E to Fragment B without removing Fragment D and C also not adding again Fragment E. And Is possible that index will not effected via this because I need to open Fragment E if Press back else from B > C > D will work as working as before.

Thanks

Akarsh M
  • 1,629
  • 2
  • 24
  • 47
  • replace fragment E with Fragment B – Harish_N Nov 06 '15 at 07:31
  • AFAIK I'm afraid that you cant. The only hack you can do is to manage your fragment by yourself, such as: creating your own list of fragment, then override `onBackPress` to act as poping from backstack. – Kingfisher Phuoc Nov 06 '15 at 07:35
  • what do you want to achieve by making a stack of 5 `Fragment`s? – pskink Nov 06 '15 at 07:40
  • @pskink : I need to switch from E > B and if i do not select anything in this fragment and I press back it will take me to the fragment A but at this Time I don not want to go on "A" ... I need to open E without lossing its state .. Something like – Akarsh M Nov 06 '15 at 07:44
  • why Fragments? they are not designed for such deep navigation... why dont you use Activities? – pskink Nov 06 '15 at 07:50
  • because I have done with fragment, Now will have to do some changes – Akarsh M Nov 06 '15 at 10:19

2 Answers2

0
if (fragmentA == null) {
    fragmentA = new AFragment();
    fragmentManager.beginTransaction().add(R.id.container1, fragmentA).commitAllowingStateLoss();
} else {
    if (fragmentA != null) 
        fragmentManager.beginTransaction().show(fragmentA).commit();
    if (fragmentB != null)
        fragmentManager.beginTransaction().hide(fragmentB).commit();
    if (fragmentC != null)
         fragmentManager.beginTransaction().hide(fragmentC).commit();
    if (fragmentD != null)
        fragmentManager.beginTransaction().hide(fragmentD).commit();
}

You can try like that, declare Fragment if it is null, then show or hide depend on your purposes . container1 is layout of this

<FrameLayout
    android:id="@+id/container1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/lnBottomButton" />

This is main view that contains all your fragments in the Activity

BaDo
  • 540
  • 8
  • 19
  • You don't care about index, just check which fragment you want to show, In my sample code, it is used to show AFragment, if FragmentB, you just do like that, hide all and show B only – BaDo Nov 06 '15 at 07:50
  • show not working to me .. Please elaborate with some snippet .. thanks – Akarsh M Nov 06 '15 at 10:22
0

If you are using ViewPager than you can implement ViewPager.OnPageChangeListener and in iots onPageSelected you will set your logic to change the page using if else

@Override
    public void onPageSelected(int position) {
        // set you logic here and set the relevant Fragment to the pager
}
Zubair Akber
  • 2,760
  • 13
  • 30
  • I am not using view pager ... It just like changing the view .. But I like the way you wrote... May be will give some way to manage it.. I will update you .. Thanks – Akarsh M Nov 06 '15 at 07:45
  • i think you should move to view pager, it is easy to implement – Zubair Akber Nov 06 '15 at 07:46