nowinandroid icon indicating copy to clipboard operation
nowinandroid copied to clipboard

[FR]: Use navigateUp() Instead of popBackStack() for Toolbar Up Button Click

Open iamjosephmj opened this issue 10 months ago • 3 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Describe the problem

I recently reviewed the principles of navigation in Compose. From what I understand, the top app bar’s “Up” navigation button should call navController.navigateUp() rather than popBackStack(), which is intended for handling the device’s back press.

However, in the NiaNavHost, the onBackClick callbacks in various screens are currently using popBackStack(). My understanding is that onBackClick is hoisted to handle the toolbar’s “Up” button clicks in these screens.

The current implementation using popBackStack() works as expected. However, I would like to understand whether using navigateUp() might be a more appropriate approach.

Describe the solution

Current

topicScreen(
    showBackButton = true,
    onBackClick = navController::popBackStack,
    onTopicClick = navController::navigateToTopic,
)

Proposed

topicScreen(
    showBackButton = true,
    onNavigateUp = navController::navigateUp,
    onTopicClick = navController::navigateToTopic,
)

Additional context

No response

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

iamjosephmj avatar Jan 26 '25 01:01 iamjosephmj

if anyone confirms i can work on it

akash0228 avatar Jan 31 '25 08:01 akash0228

Thanks @akash0228, I already have a PR line-up after I created the issue, I was also waiting for the confirmation, cheers ✨

iamjosephmj avatar Jan 31 '25 08:01 iamjosephmj

If user arrives on the screen through in-app, then both popBackStack() and navigateUp() works identical. But if user arrives from another app using deeplink from different app then this happens:

  • navigateUp() will leave your app and return to the app that navigates to the deep link in your app.
  • popBackStack() will attempt to go back one step in your backstack, and will not do anything if there is no backstack entry.

So, I think navigateUp() would be preferred choice for handling back button. If it needs to be resolved, I can work on it.

gh-shujauddin avatar Mar 21 '25 11:03 gh-shujauddin