Fixed
Status Update
Comments
il...@google.com <il...@google.com> #2
I was able to reproduce this - thanks for the great sample project!
It seems like the FragmentManager is moving the newly resumed Fragment A through all the lifecycle events before it tells the FragmentNavigator that you hit the back button. This means that from NavController's perspective, you're still on C when onResume() of A runs, hence the crash.
You can work around this by overriding onBackPressed() in your Activity:
override fun onBackPressed() {
if (!findNavController(R.id.my_nav_host_fragment).popBackStack()) {
super.onBackPressed()
}
}
By doing this, you ensure that FragmentNavigator will update its internal state before popping the Fragment and running the lifecycle methods in Fragment A.
I'll see if there's something more we can do on our end to avoid the need for a work around.
It seems like the FragmentManager is moving the newly resumed Fragment A through all the lifecycle events before it tells the FragmentNavigator that you hit the back button. This means that from NavController's perspective, you're still on C when onResume() of A runs, hence the crash.
You can work around this by overriding onBackPressed() in your Activity:
override fun onBackPressed() {
if (!findNavController(R.id.my_nav_host_fragment).popBackStack()) {
super.onBackPressed()
}
}
By doing this, you ensure that FragmentNavigator will update its internal state before popping the Fragment and running the lifecycle methods in Fragment A.
I'll see if there's something more we can do on our end to avoid the need for a work around.
th...@gmail.com <th...@gmail.com> #3
Thank you for quick response. Had a workaround, but this is a lot cleaner.
il...@google.com <il...@google.com> #4
There's nothing else we can do while Navigation still depends on the Support Library, but I'll leave this open for when we move to AndroidX and can use https://developer.android.com/reference/androidx/activity/OnBackPressedCallback to intercept the back button before Fragments and correctly call navController().popBackStack().
If you've already moved to AndroidX, the onBackPressed() solution above or the same thing with an OnBackPressedCallback if you're using Fragment 1.1.0-alpha01 or higher would work.
If you've already moved to AndroidX, the onBackPressed() solution above or the same thing with an OnBackPressedCallback if you're using Fragment 1.1.0-alpha01 or higher would work.
ap...@google.com <ap...@google.com> #5
Project: platform/frameworks/support
Branch: androidx-master-dev
commit d0c85eb6e595cfd4383ef345e97e1b8d6acd0a44
Author: Ian Lake <ilake@google.com>
Date: Thu Apr 18 10:40:16 2019
Have each Fragment register its own back pressed callback
By splitting the OnBackPressedCallback apart at
a FragmentManager level, we can ensure that the
appropriate Lifecycle is used for each callback,
allowing developers to interleave the Fragment
back stack management with their own custom
callbacks.
Test: updated PrimaryNavFragmentTest tests
BUG: 111598096
Change-Id: I22cc38794c1ee20da370f1843d667b34a9529c22
M activity/api/1.0.0-alpha07.txt
M activity/api/current.txt
M activity/src/main/java/androidx/activity/ComponentActivity.java
A activity/src/main/java/androidx/activity/OnBackPressedDispatcherOwner.java
M fragment/src/androidTest/java/androidx/fragment/app/PrimaryNavFragmentTest.kt
M fragment/src/main/java/androidx/fragment/app/FragmentActivity.java
M fragment/src/main/java/androidx/fragment/app/FragmentManagerImpl.java
https://android-review.googlesource.com/947823
https://goto.google.com/android-sha1/d0c85eb6e595cfd4383ef345e97e1b8d6acd0a44
Branch: androidx-master-dev
commit d0c85eb6e595cfd4383ef345e97e1b8d6acd0a44
Author: Ian Lake <ilake@google.com>
Date: Thu Apr 18 10:40:16 2019
Have each Fragment register its own back pressed callback
By splitting the OnBackPressedCallback apart at
a FragmentManager level, we can ensure that the
appropriate Lifecycle is used for each callback,
allowing developers to interleave the Fragment
back stack management with their own custom
callbacks.
Test: updated PrimaryNavFragmentTest tests
BUG: 111598096
Change-Id: I22cc38794c1ee20da370f1843d667b34a9529c22
M activity/api/1.0.0-alpha07.txt
M activity/api/current.txt
M activity/src/main/java/androidx/activity/ComponentActivity.java
A activity/src/main/java/androidx/activity/OnBackPressedDispatcherOwner.java
M fragment/src/androidTest/java/androidx/fragment/app/PrimaryNavFragmentTest.kt
M fragment/src/main/java/androidx/fragment/app/FragmentActivity.java
M fragment/src/main/java/androidx/fragment/app/FragmentManagerImpl.java
ap...@google.com <ap...@google.com> #6
Project: platform/frameworks/support
Branch: androidx-master-dev
commit d5a1c250e6e3471297f64e116a46d1a3a5c9db6c
Author: Ian Lake <ilake@google.com>
Date: Wed Apr 24 15:37:02 2019
Use OnBackPressedCallback in NavController
Replace the brittle OnBackStackChangedListener
with an OnBackPressedCallback registered at the
NavController level which works on all Navigators.
It also helps fix a timing issue where the state
of the FragmentNavigator isn't updated after a Fragment
is popped off the stack, leading to a mismatch in
expected actions available if the newly visible Fragment
calls navigate() in a lifecycle method.
Test: updated tests, new OnBackPressedTest
BUG: 111598096
Change-Id: Id679ea6ac9b238240649656fc0796552d6191757
M navigation/common/api/restricted_2.1.0-alpha03.txt
M navigation/common/api/restricted_current.txt
M navigation/common/src/main/java/androidx/navigation/Navigator.java
M navigation/fragment/src/androidTest/AndroidManifest.xml
M navigation/fragment/src/androidTest/java/androidx/navigation/fragment/FragmentNavigatorTest.kt
A navigation/fragment/src/androidTest/java/androidx/navigation/fragment/OnBackPressedTest.kt
M navigation/fragment/src/androidTest/java/androidx/navigation/fragment/test/EmptyFragment.kt
A navigation/fragment/src/androidTest/java/androidx/navigation/fragment/test/NavigationActivity.kt
A navigation/fragment/src/androidTest/res/navigation/nav_simple.xml
M navigation/fragment/src/main/java/androidx/navigation/fragment/FragmentNavigator.java
M navigation/fragment/src/main/java/androidx/navigation/fragment/NavHostFragment.java
M navigation/runtime/api/2.1.0-alpha03.txt
M navigation/runtime/api/current.txt
M navigation/runtime/build.gradle
M navigation/runtime/src/androidTest/java/androidx/navigation/NavControllerTest.kt
M navigation/runtime/src/main/java/androidx/navigation/NavController.java
M navigation/runtime/src/main/java/androidx/navigation/NavHost.java
https://android-review.googlesource.com/951629
https://goto.google.com/android-sha1/d5a1c250e6e3471297f64e116a46d1a3a5c9db6c
Branch: androidx-master-dev
commit d5a1c250e6e3471297f64e116a46d1a3a5c9db6c
Author: Ian Lake <ilake@google.com>
Date: Wed Apr 24 15:37:02 2019
Use OnBackPressedCallback in NavController
Replace the brittle OnBackStackChangedListener
with an OnBackPressedCallback registered at the
NavController level which works on all Navigators.
It also helps fix a timing issue where the state
of the FragmentNavigator isn't updated after a Fragment
is popped off the stack, leading to a mismatch in
expected actions available if the newly visible Fragment
calls navigate() in a lifecycle method.
Test: updated tests, new OnBackPressedTest
BUG: 111598096
Change-Id: Id679ea6ac9b238240649656fc0796552d6191757
M navigation/common/api/restricted_2.1.0-alpha03.txt
M navigation/common/api/restricted_current.txt
M navigation/common/src/main/java/androidx/navigation/Navigator.java
M navigation/fragment/src/androidTest/AndroidManifest.xml
M navigation/fragment/src/androidTest/java/androidx/navigation/fragment/FragmentNavigatorTest.kt
A navigation/fragment/src/androidTest/java/androidx/navigation/fragment/OnBackPressedTest.kt
M navigation/fragment/src/androidTest/java/androidx/navigation/fragment/test/EmptyFragment.kt
A navigation/fragment/src/androidTest/java/androidx/navigation/fragment/test/NavigationActivity.kt
A navigation/fragment/src/androidTest/res/navigation/nav_simple.xml
M navigation/fragment/src/main/java/androidx/navigation/fragment/FragmentNavigator.java
M navigation/fragment/src/main/java/androidx/navigation/fragment/NavHostFragment.java
M navigation/runtime/api/2.1.0-alpha03.txt
M navigation/runtime/api/current.txt
M navigation/runtime/build.gradle
M navigation/runtime/src/androidTest/java/androidx/navigation/NavControllerTest.kt
M navigation/runtime/src/main/java/androidx/navigation/NavController.java
M navigation/runtime/src/main/java/androidx/navigation/NavHost.java
il...@google.com <il...@google.com> #7
NavController now offers new APIs to hook up to an OnBackPressedDispatcher: https://developer.android.com/guide/navigation/navigation-custom-back
Which allows NavController to intercept the back button before the FragmentManager, ensuring that we update our state before going through Lifecycle events and fixing this edge case.
This behavior is enabled by default if you're using NavHostFragment, but custom NavHosts can call through to this same API for their own Navigators and not try to intercept the back button themselves.
Which allows NavController to intercept the back button before the FragmentManager, ensuring that we update our state before going through Lifecycle events and fixing this edge case.
This behavior is enabled by default if you're using NavHostFragment, but custom NavHosts can call through to this same API for their own Navigators and not try to intercept the back button themselves.
il...@google.com <il...@google.com> #8
This will be available in Navigation 2.1.0-alpha03.
Description
Version used: 1.0.0-alpha03
Devices/Android versions reproduced on: All
Trying to navigate in onResume of a fragment is causing a crash if the fragment was popped to.
The attached project consists of three fragments: A, B, C.
A is the home fragment and has conditional navigation to B. The conditional navigation will be triggered when the app is started, and after fragment C has been shown.
When navigating to B from A at app start there are no issues. Going back to A, then tapping the button to navigate to C, and then going back will trigger the conditional navigation again and cause a crash.
Same problem exists in alpha02.