3

So for example I have two fragments:

  • start fragment which is used as start destination of my nav grapth
  • fragment for a deep link

When I open link from email it opens my app activity (recreats it if was already opened), then opens start destionation fragment and then opens deep link fragment and when I press back button it returns to start destionation fragment but I don't want such behaviour, I want it to close app instead, so fragment deep link behave as a start destination fragment

user924
  • 8,146
  • 7
  • 57
  • 139

1 Answers1

1

You can do this in several ways:

  1. If you can change your start destination, use the deeplink fragment as start destination, but I guess you have some logic in your start destination and want to navigate to the deeplink programmatically under specified conditions. In that case this solution is not applicable.

  2. by popping the backstack before navigating programmatically to the deeplink:

   // remove start destination fragment
   findNavController().popBackStack()
   findNavController().navigate(Uri.parse(<your_deeplink>))
  1. By killing the activity from the deeplink fragment. Listen for onBackPressDispatcher in the fragment, and call requireActivity().finish() from the fragment to kill the Activity. This medium post explains in the detail the onBackPressDispatcher callback.
dnhyde
  • 1,265
  • 2
  • 14
  • 24
  • Navigation Component automatically starts deep link fragment, so there is no code from my side where I navigate to deeplink, but we can detect if `intent.data` of activity is not empty and equals a specific deeplink then we can set fragment for that deep ink as start destination and navigation component will work ok – user924 Jul 29 '21 at 11:34
  • Oh right! In your case maybe the simplest thing could be closing the Activity intercepting the onBackPress at fragment level if killing the Activity is acceptable – dnhyde Jul 29 '21 at 12:15