20

Using Android Architecture's Navigation component, I have the following navigation graph

-> [Home] -> [Articles List] -> [Specific Article]

I also have a deeplink to [Specific Article]. When it is opened, navigating up currently goes to [Home].

I'd like to synthesise a backstack such that navigating up instead goes back to [Articles List] (and then on to [Home] if navigating again).

What is the Navigation way of doing this?

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
Tom
  • 6,946
  • 2
  • 47
  • 63
  • Did anyone manage to resolve such issue ? I tried with nested graph , but in such case Navigation component could not recognize the action from nested fragments – wojciech_maciejewski Jan 06 '20 at 15:49

3 Answers3

15

Per the NavDeepLinkBuilder documentation, Navigation uses the startDestination of the destination for the synthetic back stack. If you Group destinations into a nested navigation graph, both the startDestination of the nested graph and the startDestination of the root graph are added to the back stack. This gives you the ability to have [Articles List] as the startDestination of the nested graph to add it to your back stack.

However, it is strongly recommended to keep your synthetic back stack as small as possible - while a depth of 2 or 3 (as here) is fine, it is not recommended to go much beyond that level to avoid cases where users have to repeatedly tap and tap the back button to get back to the launcher.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Cheers! I'd expect users to press home if they wanted to go to the launcher (or multitask if they want to go to a previous app). You're right it shouldn't be crazy deep though. – Tom May 15 '18 at 20:05
  • This approach actually appears to have broken deeplinking. The backstack is properly formed (Home+ArticlesList) but now Article isn't opening at all! I'm opening an issue as the doc implies my original approach should work. – Tom May 15 '18 at 21:24
  • Thanks for the bug and sample project, I'll take a look :) – ianhanniballake May 15 '18 at 21:29
  • Cheers, I'll mark your answer as correct for now - please comment/whatever if what is correct changes after the bug is resolved. – Tom May 16 '18 at 00:50
  • 2
    Yeah there should be easy backstack filling function like addAction() - called multiple times and then it should build such a backstack with any actions and destinations – Michał Ziobro Aug 03 '18 at 12:05
  • 1
    Hi @ianhanniballake, the documentation also says : 'A user can enter an app at the start destination and navigate to a destination. A user can also use a deep link, if available, to navigate to the same destination. In both of these cases, the navigation stack should have the same stack of destinations.' Now if a deeplink is used, a Back will take the user from [Article detail] to [Home], which is not the same stack as without a deeplink (detail, list, home). – Rafi Panoyan Sep 27 '18 at 14:11
  • 1
    Hey @ianhanniballake the documentation on https://developer.android.com/topic/libraries/architecture/navigation/navigation-deep-link still suggests that OP's solution should work. `This means that when a user presses the Back button from a deep link destination, they navigate back up the navigation stack just as though they entered your app from its entry point.` Went down a rabbit hole for awhile trying to figure out why this wasn't working for me. – Omega142 Feb 18 '19 at 13:52
  • Can you help me with this: https://stackoverflow.com/questions/64687574/android-navigation-url-deep-link-back-to-previous-app/64740896#64740896, thank you. – Sam Chen Nov 10 '20 at 05:01
  • Hey @ianhanniballake 2022, on version 2.5.0 this doesn't really work beyond 2-3 destinations in the stack. I have 4-level-deep nested graph. The only destinations I get in the backstack are: the destination, startDestination of the 4th level graph (destination's direct parent), and startDestination of the root graph (skipped 2 levels). According to docs, I should also expect to see the startDestinations from levels 2 and 3? Am I missing something? Thanks :) – Aleksander Niedziolko Mar 21 '22 at 18:19
  • 1
    @AleksanderNiedziolko - there was a bug for that in 2.5.0-alpha01 that was fixed in alpha02. If you're still seeing an issue in the latest (alpha03 at this point), please [file an issue](https://issuetracker.google.com/issues/new?component=409828&template=1093757) with a sample project that reproduces your issue. – ianhanniballake Mar 21 '22 at 18:34
  • @ianhanniballake Ah awesome, can confirm the bug is fixed thanks! :)) – Aleksander Niedziolko Mar 22 '22 at 09:55
  • 1
    @ianhanniballake Is there any way to do this for an implicit deep link? Currently I'm creating two separate deep link requests back to back to achieve this. Is there a better way? – Aldrin Joe Mathew Jan 04 '23 at 10:37
3

The documentation implies that my original solution should work.

When a user uses the Back button from a deep link destination, they navigate back up the navigation stack just as though they entered your app from the app’s entry point.

In addition, ianhanniballake's answer doesn't produce expected results (the deeplinked fragment is not opened).

I have created an issue on google's tracker for both these problems: https://issuetracker.google.com/issues/79734195

Tom
  • 6,946
  • 2
  • 47
  • 63
  • 4
    I mean, I wrote the library :P Have to fix the docs though :) – ianhanniballake May 15 '18 at 21:29
  • Lawl, I didn't realise. Cheers :D I'm really excited for what this library promises to achieve. – Tom May 16 '18 at 00:49
  • @ianhanniballake. I have a question about deeplink. Not sure if I should create another post. But the situation is I have a MainActivity with main navigation graph, its using bottom nav view, and has 4 tabs in it. In the tab#1 I have a viewpager2 and tabs with 5 pages/fragments. In the fragment#3 in this viewpager tab. I have a list of items. And upon clicking it goes to detail view. Now my question is it possible to deeplink on this detail page, and upon clicking back it shows the home screen with the bottomNavtab#1 autoselected, with the viewpager’s page#3 selected. To simulate the nav – Jai Pandit Jul 25 '21 at 05:01
0

I came across this thread while using navigation compose. The issue for me was that I called navController.popBackStack() instead of the correct navController.navigateUp(). After changing my calls to navigateUp(), everything works as expected. Maybe this helps someone with the same problem.

LN-12
  • 792
  • 8
  • 19