3

My App has only one activity and lots of fragments.

In my activty's XML, I just have a FrameLayout on which I replace/add/hide/show various fragments.

Imagine Fragment A is the first fragment the user sees when they open the app.

Click something in Fragment A to launch Fragment B and click something in Fragment B to launch Fragment C.

So the navigation is can be illustrated as follows :

Fragment A --> Fragment B --> Fragment C

I want to launch the app and show Fragment C directly from notification.

However, how can I provide back navigation from Fragment C, as such clicking back would go to Fragment B and clicking back again go to Fragment A ?

i.e How can I inject the following stack structure ?

Fragment A <-- Fragment B <-- Fragment C

rgv
  • 1,186
  • 1
  • 17
  • 39

2 Answers2

1

What you can do is - Use a notification intent in which you pass a string. In your main activity if you receive that string make a fragment stack of A, B and C. Else if you don't get the intent just continue your flow as it is.

Vaibhav Sharma
  • 2,293
  • 1
  • 17
  • 24
  • "make a fragment stack of A, B and C" that is my question, how do I do it? There are ways to create an activity stack, in order to maintain navigational integrity, when launching app from notification. – rgv May 19 '16 at 15:43
  • What I meant was you use fragmentTransaction.addToBackStack(str) when you get lets say- getIntent().hasExtra(Constant.FROM_NOTIFICATION) otherwise you go ahead as it is. – Vaibhav Sharma May 19 '16 at 17:29
  • the add to back stack method- final FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.frame_content, fragmentB); ft.addToBackStack("fragB"); ft.commit(); and then for fragment c – Vaibhav Sharma May 19 '16 at 17:29
  • Sorry, that is not stack injection. That is normally adding fragments one by one. It will cause the user to see A ,B and C being added one by one. – rgv May 19 '16 at 18:19
  • Do this in onStart. User sees Ui only once on resume has been called. – Vaibhav Sharma May 19 '16 at 18:33
  • @NoobDogg did you try? – Vaibhav Sharma May 20 '16 at 17:14
  • Yeah doesn't work, it partially does but doesn't scale, thanks for helping out. – rgv May 22 '16 at 04:12
1

Yes, you can do this. Since support library v26 you can build the stack with fragments without significant cost. In your activity make the following:

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, new FragmentA())
            .addToBackStack("fragmentA")
            .setReorderingAllowed(true)
            .commit();

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, new FragmentB())
            .addToBackStack("fragmentB")
            .setReorderingAllowed(true)
            .commit();

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, new FragmentC())
            .addToBackStack("fragmentC")
            .setReorderingAllowed(true)
            .commit();

Keep in mind that FragmentA and FragmentB will behave in a bit different way after pressing back on FragmentC due to setReorderingAllowed. onCreateView won't be called for FragmentA and FragmentB after they were added to stack, only in FragmentC onCreateView will be called. For FragmentA and FragmentB only onCreate will be called.

gabin
  • 927
  • 1
  • 10
  • 20