Bug P3
Status Update
Comments
jb...@google.com <jb...@google.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
ca...@gmail.com <ca...@gmail.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.
il...@google.com <il...@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.
Description
Version used:
Devices/Android versions reproduced on:
If this is a bug in the library, we would appreciate if you could attach:
- Sample project to trigger the issue.
- A screenrecord or screenshots showing the issue (if UI related).
Scenario:
= Screen 1: A user clicks a button to open a dialog.
= Dialog: The user enters text in a TextField and clicks the "Save" button.
= Save Button Action: When the "Save" button is clicked, the entered value is set in the SavedStateHandle of the previous screen (Screen 1), and then the app navigates back to Screen 1 using navController.popBackStack().
The code used to save the value is:
---------------------------------
navController.previousBackStackEntry?.savedStateHandle?.set("SampleText", textfieldValue)
navController.popBackStack()
--------------------------------
=Screen 1: Upon returning to Screen 1, the app tries to get the value from the SavedStateHandle of the current back stack entry:
----------------------------
val sampleText: String? = navBackStackEntry.savedStateHandle.get<String>("SampleText")
------------------------------
= Expected Behavior:
When returning to Screen 1 using popBackStack(), the screen should refresh or recombine, and the text saved in SavedStateHandle should be correctly retrieved and used in the UI.
= Actual Behavior in Jetpack Navigation 2.8.x:
After calling popBackStack(), Screen 1 does not refresh or recompose.
The value stored in the SavedStateHandle (i.e., "SampleText") is not retrieved properly, and the screen doesn't reflect the changes made in the dialog.
=Behavior in Jetpack Navigation 2.7.7 (working version):
In version 2.7.7, this behavior works as expected:
The SavedStateHandle value is properly passed back to Screen 1.
Screen 1 refreshes or recomposes, and the updated value is retrieved successfully.