Tuesday, 23 April 2019

Unclude fragment from Backstack using Navigation component

I have Fragments X, A, B, and i'm using Navigation architecture component to navigate between them.

Fragments A, B specific, but Fragment X can be any(C,D,...);

Fragments A and B from Bottom Navigation and their "navigations icons" always on the screen, it means user can go to A or B anytime from any Fragment(include A and B):


X -> A -> B

X -> B -> A

A -> B -> X

A -> B -> A

//another ways


My problem about this case:

X -> A -> B -> A -> B -> ?

If user started from X, reached ? and begin to go back by "back" button, he goes throw A,B several times:

User pressed back:

? -> B -> A -> B -> A -> X

But I want "to exclude" fragments from backstack if they already on it:

? -> A -> B -> X

If user navigate:

X -> A -> B -> A

I want to see:

A -> B -> X

Not:

A -> B -> A -> X

I'm trying to do it with Pop To, but it can return me on one one concrete Fragment only. I need to return on my started X Fragment, not hardcoded. Inclusive and Single top is not about it.

I'm not sure i can do it with basic Navigation component, so i need your advice. If i can't do it, what way should i use? Is there any good practices about it?


UPD:

I'm using global points to navigate between Fragments. It's how my navigation looks like:

enter image description here

The right|bottom chain is X, i can navigate from any of it to to not chanied fragments using bottom navigation. It's Single Activity app, to navigate i'm using just:

   //Using global points
   findNavController(R.id.host).navigate(R.id.toLibrary)



from Unclude fragment from Backstack using Navigation component

No comments:

Post a Comment