1

My app needs to perform some view resizing after the layout is done. It's currently done in onWindowFocusChanged().

It's not working as intended on Windows 11 (using Windows Subsystem for Android, WSA). During manual window resizing, the Activity gets destroyed and recreated a few times, but:

  • onWindowFocusChanged() is never called during or after the resizing
  • the Activity is not recreated at the exact end of the resizing, so posting a runnable to the UI queue is not an option: the view's dimensions would still be wrong at this point.

I have noticed that toggling on the WSA compatibility setting Disable smooth resize actually solves the issue: nothing happens during the resizing, but onWindowFocusChanged() is called when the resizing is over.

Disable smoot resize toggle

Can I force this behavior programmatically?

I don't want users to be forced to change this setting to use the app normally.

Sébastien
  • 13,831
  • 10
  • 55
  • 70
  • That's not an Android thing, its a windows thing. So unless you wrote an Android app only to run on Windows (in which case- why did you do that?) there won't be a way because the entire concept doesn't exist on Android. – Gabe Sechan Nov 28 '22 at 17:27
  • I have a pretty similar issue: whenever I manually resize the app window, Activity recreation causes an app crash. I temporary solved it by enabling the other one setting in your screen "Force app to be non-resizeable" but I would avoid it, you know an how-to ? – riccardogabellone Dec 13 '22 at 10:32

1 Answers1

0

I ended up finding out that this window resizing issue was not specific to WSA: it also occurs on regular Android devices and emulators when using free-form resizable windows.

The solution is to stop relying on onWindowFocusChanged() to be notified of changes of the window size: it's an unreliable hack (see question).

Instead, I now use an OnGlobalLayoutListener, like so:

       myGlobalParentView.getViewTreeObserver()).addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                setDimensionsOfVariableViews();
            }
        });

Game views are correctly resized on every change of window size. Basic protection (check if size has changed since last call) against excessive resizing/layouts and infinite loops is implemented in setDimensionsOfVariableViews(), so we never remove the listener.

Sébastien
  • 13,831
  • 10
  • 55
  • 70