Hi, I have a bit of an issue with my rotary input....
# compose-wear
n
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:
Copy 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
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
Yes it does. Maybe I could make a video if that would help?
y
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
ok I am on
Copy code
wear_compose_version = '1.2.0-alpha07'
y
rememberActiveFocusRequester()
n
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
Thanks, I was clearly looking in the wrong place lol
y
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
Ok brilliant, many thanks!
This works fine! 😊
378 Views