Bug P3
Status Update
Comments
es...@gmail.com <es...@gmail.com> #2
Project: platform/frameworks/support
Branch: androidx-master-dev
commit 85c1de7395f07e3256a50706d4f619e654624250
Author: Ian Lake <ilake@google.com>
Date: Fri Apr 19 13:54:21 2019
Ensure Fragment OnBackPressedCallbacks take priority
As FragmentController is not yet driven by a
LifecycleObserver ( b/127528777 ), LifecycleObservers on
Fragments are not necessarily nested within
LifecycleObservers registered at the Activity level.
This can lead to cases where a Fragment is started
before the Activity's Lifecycle is started.
In the case of FragmentManager's usage of Lifecycle to
add OnBackPressedCallbacks, that nesting is critical
to ensure the ordering of callbacks. By creating a
host level Lifecycle that is exactly nested outside
the callbacks to FragmentController, we can ensure
the proper nesting in callbacks.
Test: added test passes
Change-Id: I958d2389c90dcd9d157c6c3d30dffb689ed40c62
M fragment/src/androidTest/AndroidManifest.xml
M fragment/src/androidTest/java/androidx/fragment/app/OnBackPressedCallbackTest.kt
M fragment/src/main/java/androidx/fragment/app/FragmentActivity.java
https://android-review.googlesource.com/948209
https://goto.google.com/android-sha1/85c1de7395f07e3256a50706d4f619e654624250
Branch: androidx-master-dev
commit 85c1de7395f07e3256a50706d4f619e654624250
Author: Ian Lake <ilake@google.com>
Date: Fri Apr 19 13:54:21 2019
Ensure Fragment OnBackPressedCallbacks take priority
As FragmentController is not yet driven by a
LifecycleObserver (
Fragments are not necessarily nested within
LifecycleObservers registered at the Activity level.
This can lead to cases where a Fragment is started
before the Activity's Lifecycle is started.
In the case of FragmentManager's usage of Lifecycle to
add OnBackPressedCallbacks, that nesting is critical
to ensure the ordering of callbacks. By creating a
host level Lifecycle that is exactly nested outside
the callbacks to FragmentController, we can ensure
the proper nesting in callbacks.
Test: added test passes
Change-Id: I958d2389c90dcd9d157c6c3d30dffb689ed40c62
M fragment/src/androidTest/AndroidManifest.xml
M fragment/src/androidTest/java/androidx/fragment/app/OnBackPressedCallbackTest.kt
M fragment/src/main/java/androidx/fragment/app/FragmentActivity.java
if...@thescore.com <if...@thescore.com> #3
FWIW, I think this is a fantastic idea and would go a long way in bringing sanity to the world of lifecycle. As it would change the ordering between Activity lifecycle methods and Fragment lifecycle methods, this will almost certainly be an opt-in flag set at the FragmentActivity level.
jb...@google.com <jb...@google.com> #4
It's worth noting, that I actually expected the Activity to fire Lifecycle.Event.ON_CREATE from `Activity.onCreate` which is what the fragmentLifecycleRegistry does.
Not sure how this would be exposed, but driving Activity.getLifecycle events from the ReportFragment which is actually a different lifecycle to that Activity is very odd.
Not sure on the API, but a getActivityLifecycle and getFragmentLifecycle would be nice in this situation, as I actually want my lifecycle events to fire IN onCreate not in onStart.
Not sure how this would be exposed, but driving Activity.getLifecycle events from the ReportFragment which is actually a different lifecycle to that Activity is very odd.
Not sure on the API, but a getActivityLifecycle and getFragmentLifecycle would be nice in this situation, as I actually want my lifecycle events to fire IN onCreate not in onStart.
if...@thescore.com <if...@thescore.com> #5
Hi, any news?
Description
Version used: 2.8.0
Devices/Android versions reproduced on: Android 12, 13 and 14, Samsung, Motorola and other devices.
I'm noticing an issue in our production app and I unfortunately can't reproduce it. The stacktrace is below and it references Android's `NavController` code.
Stacktrace:
```
Fatal Exception: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
at java.util.Objects.checkIndex(Objects.java:359)
at java.util.ArrayList.get(ArrayList.java:434)
at androidx.navigation.NavController.tryRelaunchUpToExplicitStack(NavController.kt:990)
at androidx.navigation.NavController.navigateUp(NavController.kt:935)
at <>.MainActivity.handleBackPressed(MainActivity.kt:345)
at <>.BindingActivity$onCreate$1.handleOnBackPressed(BindingActivity.kt:19)
at androidx.activity.OnBackPressedDispatcher.onBackPressed(OnBackPressedDispatcher.kt:276)
at androidx.activity.OnBackPressedDispatcher$5.invoke(OnBackPressedDispatcher.kt:134)
at androidx.activity.OnBackPressedDispatcher$5.invoke(OnBackPressedDispatcher.java:134)
at androidx.activity.OnBackPressedDispatcher$Api33Impl.createOnBackInvokedCallback$lambda$0(OnBackPressedDispatcher.java:369)
at android.view.ViewRootImpl$NativePreImeInputStage.onProcess(ViewRootImpl.java:6285)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5973)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:9131)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:9082)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:9037)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:9263)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:267)
at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
at android.os.MessageQueue.next(MessageQueue.java:335)
at android.os.Looper.loopOnce(Looper.java:161)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7941)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:569)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1019)
```
The implementation of `MainActivity.handleBackPressed()` calls `supportFragmentManager.fragments.filterIsInstance<NavHostFragment>().lastOrNull()?.navController?.navigateUp()` and that seems to trigger the crash.
`BindingActivity` adds a callback to `onBackPressedDispatcher` via `onBackPressedDispatcher.addCallback(...)` in `onCreate(...)`.
I'm not exactly sure if the bug is on our end or if it's a navigation library bug. I unfortunately haven't been able to reproduce this, so I can't attach a sample project or a screen recording.
Any insight to the solution is appreciated.