Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if screen is in MultiWindowMode without activity

Is there any way to find out if screen is split if I have no access to Activity? (Structurally I can't call isInMultiWindowMode method.

I see that default Activity#isInMultiWindowMode() implementation is:

public boolean isInMultiWindowMode() {
    try {
        return ActivityManagerNative.getDefault().isInMultiWindowMode(mToken);
    } catch (RemoteException e) {
    }
    return false;
}

Is there any workaround ?

like image 631
Andrii Abramov Avatar asked Aug 30 '25 18:08

Andrii Abramov


2 Answers

I think the only way to do this without an Activity is by using an AccessibilityService that has the permissions to get the list of windows currently displayed and check if there's a window whose type is AccessibilityWindowInfo.TYPE_SPLIT_SCREEN_DIVIDER.

For example, you could have the following method to do so:

private boolean inSplitScreenMode(List<AccessibilityWindowInfo> windows) {
    for (AccessibilityWindowInfo window : windows) {
        if (window.getType() == AccessibilityWindowInfo.TYPE_SPLIT_SCREEN_DIVIDER) {
            return true;
        }
    }
    return false;
}

check this method when receiving window state changed accessibility events

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if ((event.getEventType() & AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) != 0) {
        if (inSplitScreenMode(getWindows()) {
            Log.d(TAG, "Split screen mode detected");
        } else {
            Log.d(TAG, "No split screen");
        }
    }
}
like image 83
Alex Ionescu Avatar answered Sep 02 '25 07:09

Alex Ionescu


Inside Fragment you can use

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    if (((Activity)getContext()).isInMultiWindowMode()){
        // ...
    }
}
like image 23
GMG Avatar answered Sep 02 '25 07:09

GMG