1
0

More than 3 years have passed since last update.

Navigation 2.3.1でonNavDestinationSelectedを使ってActivityに遷移できない

Last updated at Posted at 2020-10-22

AndroidX の Navigation 2.3.1MenuItem#onNavDestinationSelected を使用して Activity へ遷移させると遷移が正しくできない問題に遭遇しました。
Fragment への遷移ではこの現象は発生しません。

Workaround

Issue Tracker に報告したところ、バグ扱いになったのでライブラリ側で対応してくれるのを待ってもいいかもしれません。
https://issuetracker.google.com/issues/171364502

Activity への遷移の時は navigate を使用するようにすれば回避することができます。

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    val navController = findNavController(R.id.nav_host_fragment)
    if (navController.currentDestination?.parent?.findNode(item.itemId) is ActivityNavigator.Destination) {
        navController.navigate(item.itemId)
        return true
    }
    return item.onNavDestinationSelected(navController) ||
            super.onOptionsItemSelected(item)
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0