wear-ospreferenceactivitypreferencefragmentpreferencescreenwatchface

For a Wear Os watchface/app, how can I request focus for the current preference screen?


I have a Wear OS app (watch face) where the user can open a preference activity (PreferenceScreen based on PreferenceFragment) for all the user settings. I am trying to enable rotary input for the watch knob or rotary bezel, but haven't been able to.

So far I found out that the issue is related with missing focus. So, what is the proper way to request focus when the preference screen is created?


Solution

  • Finally I found a solution: in my preferenceFragment I had to override onActivityCreated and request the focus on the current view. (Seems to also work when using preferenceFragmentCompat).

            @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            if (getView() != null) getView().requestFocus(); // ENABLES ROTARY INPUT
        }
    

    The only thing is that rotary input stops when opening a nested subsection in the preference screen. But it reestablishes when coming back. This seems to be normal as described in https://developer.android.com/training/wearables/user-input/rotary-input: "If your activity contains multiple scrollable views, choose one to focus using the <requestFocus /> tag. Nested scrolling is not supported with the rotating side button."