Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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.

Just set clearTask to "true" on your action. But your use case is going against the concept of the navigation.

https://developer.android.com/topic/libraries/architecture/navigation/navigation-principles#the_app_should_have_a_fixed_starting_destination

Apps have a fixed destination which is the screen the user sees when they launch your app from the launcher. This destination should also be the last screen the user sees when they return to the launcher after pressing the back button .

clearTask! It was staring me in the face.... thanks! As for your comment about the fixed destination, I don't want to get into my exact use case, but imagine that mainFragment is a license agreement fragment. You don't want a user navigating back through that. On the next start, I'll detect that the license is already accepted and immediately navigate to the newFragment. If you have a better way to accomplish this, I'm all ears! Jeremy Figgins Jun 8, 2018 at 14:54 You can use conditional navigation. Your newFragment will be the start destination, and there you will check if user need to see license agreement, if yes navigate him to mainFragment. developer.android.com/topic/libraries/architecture/navigation/… Alex Jun 8, 2018 at 15:09 It will not go against the concept of navigation if your navhost activity isn't your launch activity. Gabriel Vasconcelos Jan 20, 2020 at 20:39 Also, this quote doesn't account for many valid use cases, such as deeplinking or quick actions from widgets etc. You wouldn't want to route your users through half of your app just to get back to what they were doing before tapping that link or widget button. Conditional navigation solves some of those things, but it also wouldn't conform to the quoted "principle" as the start often isn't "seen" by the user. I might be nitpicking, but it serves the point to illustrate that the quote is more a loose (and in many cases recommendable) guideline than a set-in-stone axiomatic principle. ubuntudroid Nov 4, 2022 at 16:01

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.

  • Select the action from the Activity to be finished, in navigation graph.
  • Look for drop down for Pop To .
  • Select the fragment(i.e. the navHostFragment of the activity to be finished).
  • 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!).
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question . Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers .