https://kotlinlang.org logo
Title
n

Nat Strangerweather

04/12/2023, 4:06 PM
Hi, I have a bit of an issue with my rotary input. My app crashes, with this friendly error message: FocusRequester is not initialized. Here are some possible fixes: 1. Remember the FocusRequester: val focusRequester = remember { FocusRequester() } 2. Did you forget to add a Modifier.focusRequester() ? 3. Are you attempting to request focus during composition? Focus requests should be made in response to some event. Eg Modifier.clickable { focusRequester.requestFocus() } 1 and 2 are done. As for 3, that may be the problem, but I am not sure what I am doing wrong. Please could someone have a look at my code in thread?
My code:
val focusRequester = remember { FocusRequester() }
ScalingLazyColumn(
                state = scalingLazyListState,
                modifier = Modifier
                    .fillMaxSize()
                    .onRotaryScrollEvent {
                        scope.launch {
                            scalingLazyListState.scrollBy(it.verticalScrollPixels)
                        }
                        true
                    }
                    .focusRequester(focusRequester)
                    .focusable(),
                anchorType = ScalingLazyListAnchorType.ItemStart,
                contentPadding = PaddingValues(
                    horizontal = 8.dp,
                ),
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {...}
 LaunchedEffect(key1 = Unit) {
            focusRequester.requestFocus()
        }
This error happens in a very specific situation only. If I update my newsfeed and don't wait for it to load, and click on a different newsfeed while this is happening.
y

yschimke

04/12/2023, 4:42 PM
Does it have 0 items in that state? But I don't see why that would cause it.
Does it happen while that screen with the SLC is now in the background?
n

Nat Strangerweather

04/12/2023, 4:46 PM
Yes it does. Maybe I could make a video if that would help?
y

yschimke

04/12/2023, 4:48 PM
Possibly. But I think more filing a bug with this info and the video.
The other thing to try, wear compose 1.2 has some focus helpers, that mean you don't need to call requestFocus yourself.
And Horologist has a ScalingLazyColumn overload that does all this for you.
n

Nat Strangerweather

04/12/2023, 4:50 PM
ok I am on
wear_compose_version = '1.2.0-alpha07'
y

yschimke

04/12/2023, 4:51 PM
rememberActiveFocusRequester()
n

Nat Strangerweather

04/12/2023, 4:51 PM
Thanks, will give this a shot.
So I have the same problem with Compose 1.2 using
rememberActiveFocusRequester()
I'd be happy to try the Horologist ScalingLazyColumn, but is there an example that I can use?
These all get passed in the column state from the navigation scaffold WearNavScaffold.
n

Nat Strangerweather

04/12/2023, 7:19 PM
Thanks, I was clearly looking in the wrong place lol
y

yschimke

04/12/2023, 7:19 PM
But if you want to try it for a single screen just pass in
ScalingLazyColumnDefaults.belowTimeText().create()
It handles the RSB scrolling for you.
If you want it wired up to the TimeText and position indicator, you can use that WearNavScaffold I mentioned, like https://github.com/google/horologist/blob/main/sample/src/main/java/com/google/android/horologist/sample/SampleWearApp.kt
n

Nat Strangerweather

04/12/2023, 7:22 PM
Ok brilliant, many thanks!
This works fine! 😊