Status Update
Comments
ma...@google.com <ma...@google.com>
to...@gmail.com <to...@gmail.com> #2
We have some support in androidx.compose.ui.autofill
Leaving this bug open in case Ralston wants to add more info
an...@google.com <an...@google.com> #3
I found an example
D/Autofill Status: Autofill popup isn't shown because autofill is not available.
Did you set up autofill?
1. Go to Settings > System > Languages&input > Advanced > Autofill Service
2. Pick a service
Did you add an account?
1. Go to Settings > System > Languages&input > Advanced
2. Click on the settings icon next to the Autofill Service
3. Add your account
Is this a bug on your side or do the app developers of these password managers need to change their implementation?
an...@google.com <an...@google.com> #5
to...@gmail.com <to...@gmail.com> #6
to...@gmail.com <to...@gmail.com> #7
to...@gmail.com <to...@gmail.com> #8
I setup this modifier:
@OptIn(ExperimentalComposeUiApi::class)
fun Modifier.autofill(
autofillTypes: List<AutofillType>,
onFill: ((String) -> Unit),
) = composed {
val autofill = LocalAutofill.current
val autofillNode = AutofillNode(onFill = onFill, autofillTypes = autofillTypes)
LocalAutofillTree.current += autofillNode
this.onGloballyPositioned {
autofillNode.boundingBox = it.boundsInWindow()
}.onFocusChanged { focusState ->
autofill?.run {
if (focusState.isFocused) {
requestAutofillForNode(autofillNode)
} else {
cancelAutofillForNode(autofillNode)
}
}
}
}
And use it like this:
val emailState = remember { EmailState() }
Email(
modifier = Modifier.autofill(
autofillTypes = listOf(
AutofillType.Username,
AutofillType.EmailAddress
),
onFill = { emailState.text = it },
),
emailState = emailState,
onImeAction = { onForgotPasswordSubmitted(emailState.text) }
)
With these steps, autofill works for me.
to...@gmail.com <to...@gmail.com> #10
Works perfectly fine on the latest stable and alpha versions. Did you check in my second comment if you enabled everything?
to...@gmail.com <to...@gmail.com> #11
I believe so?
I copied the modifier and then used
modifier = Modifier.autofill(
autofillTypes = listOf(
AutofillType.Username,
AutofillType.EmailAddress
),
onFill = { emailState.text = it },
)
on my text field.
an...@google.com <an...@google.com> #12
to...@gmail.com <to...@gmail.com> #13
The auto-filling works for me with the latest alpha version, but I'm never presented with the save dialog though.
I am having exactly same problem. Autofill works, but the Save dialog is never shown...
to...@gmail.com <to...@gmail.com> #14
Any updates on this? I can't get LastPass to display at all. I can ONLY get the Google Auto-fill service to work
to...@gmail.com <to...@gmail.com> #15
Same here, autofill in Jetpack Compose only works with the Google auto-fill service. When picking 1Password as the Auto-fill service in Settings, the debug log prints out:
D/Autofill Status: Autofill popup isn't shown because autofill is not available.
Did you set up autofill?
1. Go to Settings > System > Languages&input > Advanced > Autofill Service
2. Pick a service
Did you add an account?
1. Go to Settings > System > Languages&input > Advanced
2. Click on the settings icon next to the Autofill Service
3. Add your account
to...@gmail.com <to...@gmail.com> #16
an...@google.com <an...@google.com> #17
to...@gmail.com <to...@gmail.com> #18
1.3.1 won't dialog prompt to save a password for me but will prompt to autofill for google.
As a workaround, you can wrap the TextInputLayout from a layout file to an AndroidView in compose.
@Composable
fun TextFieldView(
modifier: Modifier = Modifier,
textState: MutableState<String>,
@LayoutRes layoutRes: Int,
textChanged: () -> Unit = {},
) {
AndroidView(
modifier = modifier
.fillMaxWidth(),
factory = { context ->
val layout = LayoutInflater.from(context).inflate(layoutRes, null)
layout.findViewById<TextInputEditText>(R.id.tilET).apply {
doAfterTextChanged {
textState.value = safeText
textChanged.invoke()
}
}
layout
},
update = {
}
)
}
val EditText?.safeText: String get() = this?.editableText?.toString().orEmpty().trim()
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textfield.TextInputLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/til"
style="@style/TextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tilET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="username"
android:imeOptions="actionNext"
android:inputType="textNoSuggestions"
android:selectAllOnFocus="true" />
</com.google.android.material.textfield.TextInputLayout>
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textfield.TextInputLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/til"
style="@style/TextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:endIconMode="password_toggle">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tilET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="password"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:selectAllOnFocus="true" />
</com.google.android.material.textfield.TextInputLayout>
an...@google.com <an...@google.com> #19
to...@gmail.com <to...@gmail.com> #20
an...@google.com <an...@google.com> #21
I tried using similar to above example. But its not working & following logs are getting printed.
getAutofillClient(): null on super, trying to find activity thread getAutofillClient(): none of the 1 activities on com.xx@8b0a7bc have focus No AutofillClient for com.xx on context com.xxn@8b0a7bc requestHideFillUi(null): anchor = null
Autofill popup isn't shown because autofill is not available. Did you set up autofill?
- Go to Settings > System > Languages&input > Advanced > Autofill Service
- Pick a service
Did you add an account?
- Go to Settings > System > Languages&input > Advanced
- Click on the settings icon next to the Autofill Service
- Add your account
setSessionFinished(): from ACTIVE to FINISHED; autofillableIds=null
Can anyone help
ch...@google.com <ch...@google.com> #22
Please fix 1password and compose
ch...@google.com <ch...@google.com> #23
to...@gmail.com <to...@gmail.com> #24
Any update? It would be good to fix it asap, please.
ch...@google.com <ch...@google.com> #25
Any update on this?
It can be overlooked since it is simple but one of the important convenience feature.
ap...@google.com <ap...@google.com> #26
Autofill is currently listed as "In Focus" on our
to...@gmail.com <to...@gmail.com> #27
compose bom version: 2023.06.01
AS: Giraffe 2022.3.1
an...@google.com <an...@google.com>
to...@gmail.com <to...@gmail.com> #28
Solution from AutofillManager
launches unsafe intent:
StrictMode policy violation: android.os.strictmode.UnsafeIntentLaunchViolation: Launch of unsafe intent: Intent { (has extras) }
at android.os.StrictMode.onUnsafeIntentLaunch(StrictMode.java:2329)
at android.content.Intent.prepareToLeaveProcess(Intent.java:12589)
at android.content.Intent.prepareToLeaveProcess(Intent.java:12501)
at android.app.Activity.startIntentSenderForResultInner(Activity.java:5973)
at android.app.Activity.startIntentSenderForResult(Activity.java:5915)
at android.view.autofill.AutofillClientController.autofillClientAuthenticate(AutofillClientController.java:484)
at android.view.autofill.AutofillManager.authenticate(AutofillManager.java:2433)
at android.view.autofill.AutofillManager.-$$Nest$mauthenticate(Unknown Source:0)
at android.view.autofill.AutofillManager$AutofillManagerClient.lambda$authenticate$3(AutofillManager.java:3821)
at android.view.autofill.AutofillManager$AutofillManagerClient$$ExternalSyntheticLambda3.run(Unknown Source:12)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
an...@google.com <an...@google.com> #29
We are seeing "Contents can't be autofilled" toast message, when user has other password managers that aren't google, such as Samsung Pass
ch...@google.com <ch...@google.com> #30
Could you please post some updates on this?
- it is one year in the roadmap:
https://developer.android.com/jetpack/androidx/compose-roadmap - one CL abandoned more than a year ago:
https://android-review.googlesource.com/c/platform/frameworks/support/+/2434079 - another CL is getting silent:
https://android-review.googlesource.com/c/platform/frameworks/support/+/3009614
It is sad that almost 3 years after a stable release of Compose there is still no support for Autofill.
to...@gmail.com <to...@gmail.com> #31
A quick update here -
We built autofill for 1.7 and found that we need to do some substantial rework of the semantics system before it lands (this is ongoing top priority - aiming for hopefully 1.8).
Turning it on caused a large performance drop app-wide. We're working on fixing that, and then will turn autofill on.
It's currently #1 item on all of prioritization for compose feature work. Will give more update when it's getting closer to landing.
ap...@google.com <ap...@google.com> #32
to...@gmail.com <to...@gmail.com> #33
See
to...@gmail.com <to...@gmail.com> #34
ad...@google.com <ad...@google.com> #35
The majority of autofill CLs landed this week. It seems that the next alpha should have this :) 🤞
an...@google.com <an...@google.com>
an...@google.com <an...@google.com> #36
We'd appreciate any feedback on the API shape. If you want to test the APIs, you can turn on the flag by adding ComposeUiFlags.isSemanticAutofillEnabled = true in your onCreate() method before super.onCreate() and add in the latest Snapshot version in your build.gradle file.
Please note that turning on this flag will lead to performance regressions and also know the API shape may be different when we officially release it.
Happy coding!
to...@gmail.com <to...@gmail.com> #37
to...@gmail.com <to...@gmail.com> #38
#36 are you only currently interested in API shape feedback, or should I be filing issues for non performance related bugs?
an...@google.com <an...@google.com> #39
I would love for the api shape to be the way it was with xml - a simple one lime flag
to...@gmail.com <to...@gmail.com> #40
an...@google.com <an...@google.com> #41
I agree. Adoption of the flags was not high. Adoption of this will be lower. Users blame the password manager not the apps that have not implemented the simple flags.
"I would love for the api shape to be the way it was with xml - a simple one lime flag"
to...@gmail.com <to...@gmail.com> #42
We are accepting all types of feedback, thank you so much to those who have already submitted! We hear your feedback about having autofill be one line and are working to incorporate that into the final version of Autofill in Compose along with the performance improvements. Unfortunately, we can’t communicate any specific timeframe, but stay tuned and we’ll update you here as soon as we can.
Compose Autofill will not solve the StrictModeUnsafeIntentViolation as this is a known issue that lies with the autofill framework team. We have raised this as an issue with the autofill framework team.
-Meghan
ad...@google.com <ad...@google.com> #43
Personally I care less about a 1 liner and more about versatility of the API.
The 1 liner is good if it doesn't limit what can be done.
First and foremost it should be an idiomatic API.
to...@gmail.com <to...@gmail.com> #44
With new Compose UI version :
I see a lot of Autofill enhancements
1. The old autofill APIs are deprecated. Use the new semantics-based API instead. (I943ff)
2. Rewrite requestAutofill API to exist outside of autofill manager. (Id1929)
Can you confirm that it fixes the first problem mentioned in the
"One problem is that it only works with the Google autofill service. When using any other service like Microsoft Authenticator or OneKey"
This is something important for us. so please confirm on urgent basis
Thank you
th...@gmail.com <th...@gmail.com> #45
th...@gmail.com <th...@gmail.com> #46
Hi Priti,
Thanks for reaching out. The new Autofill APIs were added to 1.8 and you should be able to use them. I tested our demo App and Autofill works with Google Password Manager and LastPass. Can you verify that you try out this sample?
@Composable
fun BasicTextFieldAutofill() {
val autofillManager = LocalAutofillManager.current
Column {
Text(text = "Enter your username and password below.")
BasicTextField(
state = remember { TextFieldState() },
modifier = Modifier.semantics { contentType = ContentType.Username},
)
BasicTextField(
state = remember { TextFieldState() },
modifier = Modifier.semantics { contentType = ContentType.Password },
)
// Submit button
Button(onClick = { autofillManager?.commit() }) { Text("Submit credentials") }
}
}
The entire demo is here:
Source:
Can you try this out and report back if it does not work?
ap...@google.com <ap...@google.com> #47
#46 Is there some mechanism in autofill to differentiate different login/password combos for the password manager? In my app they can login to different servers they own for different services and I'd like to have some ways for the password manager to only shows the credentials relevant to the current service type.
to...@gmail.com <to...@gmail.com> #48
We do not have a way to differentiate/filter credentials within the credential managers.
-Meghan
an...@google.com <an...@google.com> #49
Please check it out and let me know what you think and let me know if you have any questions
-Meghan
to...@gmail.com <to...@gmail.com> #50
#48 Then from the docs how "For example, if a user has already signed into your app through the Chrome browser on their laptop and saved their password through a credential provider, then their credentials are served to them through Autofill." works? Or how does Chrome to tell the password manager the current website he's browsing.
Like in my case the user have saved a password in Chrome for a website with an url, and in my application he configure to connect to that same website and the same url there's really no way from Compose to have that match? All I can offer to the users is that they save again all their login/password for their servers after entering them in my app, and then they will have the list of all entries for every server they add ?
an...@google.com <an...@google.com> #51
#47: Tell us more about your use case. Did this work in a view-based app but doesn't work in Compose? If it did, what are the APIs you used for views?
Is this related to using
#50: This is good feedback. The document meant to point to the fact that if you have stored credentials, you can long press on a textField and then select one of your stored passwords. But it the way it is worded makes it look like there is a way to connect the web version of an app with the android version of the app. That's a good idea. We don't have this right now, but if you file a feature request we can send it over to the autofill team.
an...@google.com <an...@google.com> #52
That use case is specific for a Compose app from start. Will stay with the basic support then.
For the feature request can do if you point me to the proper tracker category.
to...@gmail.com <to...@gmail.com> #53
I am verifying the given code with my team now
Additionally , This is the beta release (1.8.0-beta02) , when we can expect to have a stable release of for androidx.compose.ui:ui-* ?
an...@google.com <an...@google.com> #54
More information on reporting issues here:
Use this component: Android Public Tracker > App Development > Jetpack (androidx) > Autofill
to...@gmail.com <to...@gmail.com> #55
to...@gmail.com <to...@gmail.com> #56
Please let us know if you have any other issues.
-Meghan
to...@gmail.com <to...@gmail.com> #57
Project: platform/frameworks/support
Branch: androidx-main
Author: Ralston Da Silva <
Link:
Introduce InputText Semantics Property
Expand for full commit details
Introduce InputText Semantics Property
TextFields have an EditableText semantics property which
stores the text value after output transformation. Autofill
needs access to the text value after input transformation
but before output transformation. Adding this semantics
property helps send the right information to the Autofill
service.
Bug: 395911609
Bug: 176949051
Test: Added new tests and updated existing tests
Relnote: "Added a new semantics property InputText that captures
a textfield's value before output transformation is applied."
Change-Id: Iae46a52e7fbb1a3558e897c5afebd125089befbb
Files:
- M
compose/foundation/foundation/src/androidInstrumentedTest/kotlin/androidx/compose/foundation/text/input/BasicTextFieldSemanticsTest.kt
- M
compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/input/internal/CoreTextFieldSemanticsModifier.kt
- M
compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/input/internal/TextFieldDecoratorModifier.kt
- M
compose/ui/ui-test/src/androidInstrumentedTest/kotlin/androidx/compose/ui/test/ErrorMessagesTest.kt
- M
compose/ui/ui-test/src/androidInstrumentedTest/kotlin/androidx/compose/ui/test/FindersTest.kt
- M
compose/ui/ui-test/src/androidInstrumentedTest/kotlin/androidx/compose/ui/test/assertions/AssertText.kt
- M
compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Assertions.kt
- M
compose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Filters.kt
- M
compose/ui/ui/api/current.txt
- M
compose/ui/ui/api/restricted_current.txt
- M
compose/ui/ui/bcv/native/current.txt
- M
compose/ui/ui/integration-tests/ui-demos/src/main/java/androidx/compose/ui/demos/UiDemos.kt
- M
compose/ui/ui/integration-tests/ui-demos/src/main/java/androidx/compose/ui/demos/autofill/TextFieldAutofillDemo.kt
- M
compose/ui/ui/src/androidInstrumentedTest/kotlin/androidx/compose/ui/autofill/AndroidAutofillManagerTest.kt
- M
compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/autofill/AndroidAutofillManager.android.kt
- M
compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/semantics/SemanticsProperties.kt
Hash: dee965267730e9b0b1a865a991db3515cdfc4b25
Date: Tue Feb 18 16:11:58 2025
to...@gmail.com <to...@gmail.com> #58
Tried out the latest sample but I'm unable to get the store password with commit
to work. There is no popup prompted with 1.8.0-beta02. I set the password manager to be the Google Password Manager. Phone is Pixel 8 running Android 15. Is the sample incomplete?
In some scenarios username and/or password may be generated. E.g in our usecase we have a unique number that serves as the account and password, and there is no additional details needed such as email. The user interaction to create an account would be just pressing a button "Create account" that generates a new account. With the current API I what would be the recommended way to implement this? I see there being 2 options:
- Upon successful account creation, set the semantic on a hidden TextField or TextField for login, before navigating away. (Feels it could be likely to race when set the property on a textfield just before navigating away)
- When showing the account number after a successful account creation, set it semantic
NewPassword
and then do aLaunchEffect
to commit. However this breaks the guideline of only calling commit on a button press.
to...@gmail.com <to...@gmail.com> #59
For the scenario above, it would also be nice to have an options to disable the strong password suggestion. Because as mentioned the user does not get to select their password.
ma...@gmail.com <ma...@gmail.com> #60
Edit: I finally made it work. One have to enable the "Offer to save passwords", in Google Password Manager.
Furthermore, you have to have both NewUsername
& NewPassword
content type on two separate fields, and seemingly the user have to interact with both fields in-order to offer the storage of a new login. E.g settings "username" and "password" to be default text in the field and the calling "commit" will not work, nor will it to programmatically modify these strings. An interaction with the view/keyboard has to happen.
I'm looking forward to hear if there is any way of programmatically just offering this, the current flow seems very complex for a lot of usecases.
to...@gmail.com <to...@gmail.com> #61
Thank you so much for your feedback. What sample specifically were you looking at?
The strong password option is password manager specific so while you may see certain behavior by Google Password Manager another service may give you different results.
When using Google Password Manager, the save dialog will only appear when there is a NewUsername and a NewPassword field. There also does need to be some touch or keyboard/focus input to trigger notifyViewEntered. This is not Compose specific and how its done in views as well.
Let me know if there's anything else!
-Meghan
ch...@google.com <ch...@google.com>
ch...@google.com <ch...@google.com> #62
As a reminder, do not use beta01 and beta02 in any production apps
to...@gmail.com <to...@gmail.com> #63
Hi Meghan! Thanks for the quick reply.
This is the sample I was looking at:
Regarding strong password: I see, I assume it shows this the knowing that it has to provide a password.
When using Google Password Manager, the save dialog will only appear when there is a NewUsername and a NewPassword field.
Is this the case for just Google Password Manager or all apps interacting with the autofill? Could a autofill provider choose to offer to save a Password without any user interaction and just "NewPassword" field?
We have this scenario in with our service. All our users sign up by pressing "Create account" we generate an account number to which they use as their login, users provide no email, username or password. Since the account is a generated value, we'd like to offer to save it in the users password manager so they don't forget it and have a nicer experience when logging in on other devices. I can also imagine other apps having similar solutions or generating the password to the user automatically.
Edit: I found the solution for my scenario, the usecase is not to go by the autofill service in this case, it seems like if you want to save a password w/o user interaction or with the help of the keyboard on should use the CredentialsManager.
Thanks for your reply & help Meghan
David @ MullvadVPN
an...@google.com <an...@google.com> #64
ch...@google.com <ch...@google.com>
mo...@google.com <mo...@google.com> #65
Ralston is making a large change for focus that will fix this problem. He's agreed to take this on.
da...@sonos.com <da...@sonos.com> #66
I had the same type of issues, where using a LazyRow and scrolling a lot would see performance degradation to the point where the app was unusable. I can say that the fixes from this bug have definitely fixed my issue, so thank you very much! However, now I'm seeing that my derivedStateOf calculations are getting called all the time.
I have a derivedStateOf which calculates layout information based off changes to the scroll state. For example:
class LayoutProvider(
val layoutState: State<MyLayout>
)
@Composable
fun rememberLayoutProvider(): LayoutProvider {
val layoutState = remember {
derivedStateOf {
// run some big calculations to calculate a layout
calculateLayout()
}
}
return remember { LayoutProvider(layoutState) }
}
And then many different composables use my derived state to create their own derivedStateOf to get their layout information. For example:
@Composable
fun SomeTile() {
val myLayout = remember {
derivedStateOf {
layoutProvider.getMyLayout()
}
}
do something with myLayout
}
In compose 1.2.0, this worked really well, and when the scroll state would change, the layout provider's calculation would run once, and only composables whose specific layout changed (myLayout
) would get recomposed.
Now in compose 1.3.0-beta01, the calculation in the LayoutProvider gets called a large number of times, and the composoables who have their own derivedState get recomposed even when their specific layout hasn't changed.
I noticed the change to the derivedStateOf
, with the addition of a policy, but I tried writing my own and still couldn't get the results I wanted. Can you please explain how I can go about getting these calculations to get called as I'd expect and only have the composables that have their derived state changed recompose?
Thank you
da...@sonos.com <da...@sonos.com> #67
To give some more information, when when calling derivedStateOf(structuralEqualityPolicy())
in both those places in the code above, it still behaves incorrectly. Basically, the composable SomeTile
always gets recomposed any time the scroll state changes, and only at the call site where myLayout
gets used does the derivedState calculation run (i.e. layoutProvider.getMyLayout()
. Seeing the whole point of derivedState is so that we can avoid recomposing composables on state changes that can happen frequently, this now feels broken to me. Please let me know if I'm missing something.
Thank you
da...@sonos.com <da...@sonos.com> #68
I was able to make it even more simple:
@Composable
fun SomeComposable(
scrollState: LazyListState
) {
val red by remember {
derivedStateOf(structuralEqualityPolicy()) {
// cause an observation on the scroll state
scrollState.firstVisibleItemScrollOffset != 0
}
}
Log.e("Test", "composing SomeComposable")
if (red) {
Log.e("Test", "composing a red box")
Box(modifier = Modifier.background(Color.Red).fillMaxSize())
}
else {
Log.e("Test", "composing a blue box")
Box(modifier = Modifier.background(Color.Blue).fillMaxSize())
}
}
With this code, red
should always be true except when the scroll offset is at 0, and thus the composable should only ever recompose when the scroll offset comes to zero or leaves zero. However, it recomposes on every single scroll change.
to...@gmail.com <to...@gmail.com> #69
Your last example sounds normal IMO. The scrollstate change, the SomeComposable is recomposed the state is calculated, if is called and your log is ran.
To see any benefit here you need to extract the second part to another composable that is skippable.
@Composable
fun SomeComposable(
scrollState: LazyListState
) {
val red by remember {
derivedStateOf(structuralEqualityPolicy()) {
// cause an observation on the scroll state
scrollState.firstVisibleItemScrollOffset != 0
}
}
Log.e("Test", "composing SomeComposable")
SomeComposable2(red)
}
@Composable
fun SomeComposable2(
red: Boolean
) {
if (red) {
Log.e("Test", "composing a red box")
Box(modifier = Modifier.background(Color.Red).fillMaxSize())
}
else {
Log.e("Test", "composing a blue box")
Box(modifier = Modifier.background(Color.Blue).fillMaxSize())
}
}
This will display the first composing log each time the scrollstate change, but the box composing will only happen when the derived state change as the SomeComposable2 is skippable.
da...@sonos.com <da...@sonos.com> #70
Thanks for the response, but unfortunately, that still isn't correct IMO. I understand the concept of using a composable that is skippable, but what that would accomplish is that when red
changes, SomeComposable
would not recompose, while SomeComposable2
would. That is not what I'm trying to accomplish. The issue here is that red
is not supposed to change unless the scrollable state crosses the zero boundary. So in your example, SomeComposable2
still recomposes every single time the scroll state changes, but it shouldn't.
The issue is that the red
state only gets evaluated/calculated when it is accessed, which is in the recomposition, but at this point, it's too late, as the composable is already recomposing. So DerivedState is no longer useful. In Compose 1.2.0, the red
calculation would run at the time that the scroll state changes outside the composition scope, and then would appropriately cause the composable to recompose only if the red
state actually changed.
to...@gmail.com <to...@gmail.com> #71
No in my example SomeComposable2 does not recompose because it's skippable and red state does not change.
SomeComposable will always recompose because the scrollstate change. It's up to you that your composable is made of skippable sub component so that they are not recomposed.
In the case of #68 the if is not a composable function so is not skippable. So if SomeComposable is recomposed the if will always be ran and so your log will run. If you had something different before then it was something else and not how compose is supposed to work.
In all case in my example the red state is read as it's a parameter to SomeComposable2 so it's up to date thanks to the derivedstate and so will properly change.
I use this pattern a lot in prod and it works like that since 1.0 and it still works the same in 1.3 alpha3 (and beta 1 but I'm not in prod with it due to the return of the mem leak).
In all cases your issue seems unrelated to this one so you might want to open another one if you want more traction from Google.
da...@sonos.com <da...@sonos.com> #72
da...@sonos.com <da...@sonos.com> #73
da...@sonos.com <da...@sonos.com> #74
Here is sample code that describes exactly the issue:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
OuterComposable()
}
}
}
}
@Suppress("EqualsOrHashCode")
class ZeroMap(
val zeroMap: Map<Int, Boolean>
) {
override fun equals(other: Any?): Boolean {
if (this === other) {
Log.e("StateTest", "ZeroMap is the same reference")
return true
}
if (javaClass != other?.javaClass) {
Log.e("StateTest", "ZeroMap is not the same because of javaClass")
return false
}
other as ZeroMap
if (zeroMap.size != other.zeroMap.size) {
Log.e("StateTest", "ZeroMap is not the same because trackLayouts.size (${zeroMap.size}) != other.trackLayouts.size (${other.zeroMap.size})")
return false
}
val otherEntries = other.zeroMap.entries
zeroMap.entries.forEachIndexed { index, entry ->
val otherEntry = otherEntries.elementAt(index)
if (entry.value != otherEntry.value) {
Log.e("StateTest", "ZeroMap is not the same because entry.value (${entry.value}) != otherEntry.value (${otherEntry.value}) for index $index")
return false
}
}
Log.e("StateTest", "ZeroMap is the same value")
return true
}
}
class ZeroStateProvider(
val scrollState: LazyListState,
private val zeroState: State<ZeroMap>
) {
fun isAtZero(index: Int) = zeroState.value.zeroMap[index]
}
@Composable
fun rememberZeroStateProvider(): ZeroStateProvider {
val scrollState = rememberLazyListState()
val zeroState = remember {
derivedStateOf(structuralEqualityPolicy()) {
Log.e("StateTest", "calculating zero state for scroll offset: ${scrollState.firstVisibleItemScrollOffset}")
val zeroMap = mutableMapOf<Int, Boolean>()
scrollState.layoutInfo.visibleItemsInfo.forEach {
zeroMap[it.index] = it.offset == 0
}
ZeroMap(zeroMap)
}
}
return remember {
ZeroStateProvider(scrollState, zeroState)
}
}
@Composable
fun OuterComposable(
modifier: Modifier = Modifier,
) {
Log.e("StateTest", "Composing OuterComposable")
val zeroStateProvider = rememberZeroStateProvider()
LazyRow(
state = zeroStateProvider.scrollState,
horizontalArrangement = Arrangement.spacedBy(60.dp),
verticalAlignment = Alignment.Top,
modifier = modifier
.fillMaxWidth()
) {
items(
count = 100,
) { index ->
InnerComposable(index, zeroStateProvider)
}
}
}
@Composable
private fun InnerComposable(
index: Int,
zeroStateProvider: ZeroStateProvider,
modifier: Modifier = Modifier
) {
Log.e("StateTest", "Composing InnerComposable (index=$index)")
val color by remember {
derivedStateOf(structuralEqualityPolicy()) {
Log.e("StateTest", "InnerComposable: calculating color")
when (zeroStateProvider.isAtZero(index)) {
true -> Color.Red
false,
null -> Color.Blue
}
}
}
Box(
modifier = modifier
.background(color)
.size(200.dp)
)
}
The output in Compose 1.2.0 gives:
E/StateTest: InnerComposable: calculating color
E/StateTest: calculating zero state for scroll offset: 1
E/StateTest: Composing InnerComposable (index=0)
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: Composing InnerComposable (index=4)
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: calculating zero state for scroll offset: 3
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: calculating zero state for scroll offset: 5
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: calculating zero state for scroll offset: 6
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
and in Compose 1.3.0-beta01 gives:
E/StateTest: calculating zero state for scroll offset: 2
E/StateTest: ZeroMap is not the same because entry.value (false) != otherEntry.value (true) for index 0
E/StateTest: InnerComposable: calculating color
E/StateTest: calculating zero state for scroll offset: 2
E/StateTest: ZeroMap is not the same because entry.value (false) != otherEntry.value (true) for index 0
E/StateTest: Composing InnerComposable (index=0)
E/StateTest: calculating zero state for scroll offset: 2
E/StateTest: ZeroMap is not the same because entry.value (false) != otherEntry.value (true) for index 0
E/StateTest: InnerComposable: calculating color
E/StateTest: calculating zero state for scroll offset: 2
E/StateTest: ZeroMap is not the same because entry.value (false) != otherEntry.value (true) for index 0
E/StateTest: calculating zero state for scroll offset: 2
E/StateTest: ZeroMap is not the same because entry.value (false) != otherEntry.value (true) for index 0
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: InnerComposable: calculating color
E/StateTest: calculating zero state for scroll offset: 4
E/StateTest: ZeroMap is the same value
E/StateTest: calculating zero state for scroll offset: 5
E/StateTest: ZeroMap is the same value
As you can see from this output, in Compose 1.2.0, calculating zero state for scroll offset
only appears once every time the scroll state changes, and then the inner compositions recalculate accordingly. But in Compose 1.3.0-beta01, calculating zero state for scroll offset
shows up many times every time the scroll state changes. The number of times it runs increases with the number of composables that use its state.
From the documentation of derivedStateOf, it says "The result of calculation will be cached in such a way that calling [State.value] repeatedly will not cause[calculation] to be executed multiple times". This was true prior to Compose 1.3.0-beta01, but no longer is.
Please let me know what you think. Thanks!
to...@gmail.com <to...@gmail.com> #75
As said I'm not a Googler and your issue is unrelated to the original issue. You really should open another issue.
I'd say that your issue might not be related to derivedstate at all but to the compiler generation of your ZeroStateProvider class.
I'd use
da...@sonos.com <da...@sonos.com> #76
Yes, I'm ok with creating a new issue (which I did here:
to...@gmail.com <to...@gmail.com> #77
You can easily bisect the version that introduced the issue. All the fixes for this issue are in 1.2.0. AFAIR the only missing to fix all was
Try 1.3 alpha 3 that I use in prod with no performance regression. Then alpha 2 then 1 ;)
da...@sonos.com <da...@sonos.com> #78
It was introduced by
to...@gmail.com <to...@gmail.com> #79
Small bump on this to follow from #65
Is this planned to be backported to 1.3?
With Material 3 pinned to 1.3 we can't keep 1.3 A3 and update material. And not sure they will start an 1.1 alpha pinned to 1.4 before a final 1.3 release. (Maybe you have some info about that)
to...@gmail.com <to...@gmail.com> #80
So 1.3 RC1 is cut, is it possible to have some insight on this please? On my app with tons of large lazylist this leads to very fast OOM (even faster than previous issues).
Not having a solution for 1.3 may have high impact on updating M3 / Glance and the sooner we know the easier to anticipate.
ad...@google.com <ad...@google.com> #81
1.3-rc01 hasn't been cut yet, though the commit to bump the version to rc is in the system to ready for that build cut
ap...@google.com <ap...@google.com> #82
Branch: androidx-main
commit 3bd044a0a148b16ce17eaa71ceb5b8aed1bde70a
Author: Ralston Da Silva <ralu@google.com>
Date: Thu Sep 15 13:22:36 2022
Fix for issue where ModifierLocals were not being updated when modifiers were reused
With the introduction of Modifier.Node in aosp/2108724 onModifierLocalsUpdated was
not being called with the default value when modifiers were reused. This broke
LazyListFocusMoveTest.
Verified that this CL fixes the test, and added new tests to ModifierLocalSameLayoutNodeTest.kt
Bug: 245512265
Bug: 245131728
Bug: 245363143
Bug: 230168389
Fixes: 245512265
Fixes: 245131728
Test: ./gradlew compose:f:f:cC -P android.testInstrumentationRunnerArguments.class=androidx.compose.foundation.lazy.list.LazyListFocusMoveTest
Test: ./gradlew compose:ui:ui:cC -P android.testInstrumentationRunnerArguments.class=androidx.compose.ui.modifier.ModifierLocalSameLayoutNodeTest
Change-Id: I90d10bf341c4b19a979440886f3d002f5bc8320a
M compose/ui/ui/src/androidAndroidTest/kotlin/androidx/compose/ui/modifier/ModifierLocalSameLayoutNodeTest.kt
M compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/BackwardsCompatNode.kt
to...@gmail.com <to...@gmail.com> #83
So tested snapshot 9082024 that include this and it seems to be fixed.
And I also see nice scrolling performance on large lists, maybe due to other commits.
The last question is. Are there any commits in 9082024 that won't be part of the final 1.3? Or can I expect the same performance gain in the coming 1.3 releases ?
an...@google.com <an...@google.com> #84
ra...@google.com <ra...@google.com>
ju...@gmail.com <ju...@gmail.com> #85
na...@google.com <na...@google.com> #86
This bug was linked in a change in the following release(s):
androidx.compose.ui:ui:1.3.0-rc01
na...@google.com <na...@google.com> #87
The following release(s) address this bug:
androidx.compose.runtime:runtime:1.3.0
Description
Jetpack Compose version: 1.2.0 alpha 07 Jetpack Compose component used: LazyColumn Android Studio Build: AGP 7.3.0-alpha09 Kotlin version: 1.6.20 / 1.6.21
I have not yet a repro but will need some input about where to look for to build it.
Starting recently so I guess Alpha 06 or 07 I started to have OOM issues in my app. Doing some heap dump I see the following see attached images.
The first dump is after a few scroll and normal usage of the application. The second dump is after a lot more scrolling in a single LazyColumn.
As you can see the retained size is insane for SnapshotMutableStateImpl and the app quickly OOM.
I'd need some input about what is stored in those to be able to build a repro