Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation Editor - Finish Activity?

I'm trying out Android's new Navigation Editor for the first time and I'm not sure if this is a missing feature, intentional omission, or if I'm missing something. I have two fragments and I want the first fragment to be able to navigate to the second one, but I want the activity to finish if back is pressed from either fragment.

navigation editor

With my current setup, I can navigate from mainFragment to newFragment. If I press back from the mainFragment, the activity finishes. The only piece I can't figure out is how to finish the activity when back is pressed from newFragment. I've tried every combination of Pop Behavior settings, but haven't achieved what I'm looking for.

like image 455
Jeremy Figgins Avatar asked Oct 27 '25 00:10

Jeremy Figgins


1 Answers

See the screenshot and look for Pop Behavior. This option can be used to finish activity.

Please note: Finish activity = pop the Activity off the stack.

  1. Select the action from the Activity to be finished, in navigation graph.
  2. Look for drop down for Pop To.
  3. Select the fragment(i.e. the navHostFragment of the activity to be finished).
  4. Check Inclusive option. (i.e. From current destination point- in ur case, it's an action - to and including this fragment - in ur case navHostFragment of Activity- in the stack will be popped off the stack. And that's what we need!).
like image 169
Jose Avatar answered Oct 29 '25 16:10

Jose