Home > front end >  How to close other App in multi-screen mode
How to close other App in multi-screen mode

Time:01-18

So if i click a button, google maps app opens and it splits the screen with my app, so far so good. But if i click the button again, i want to close google maps (or at least send to background). In all cases I tried so far my app closes and maps gets the full screen. If i do the "back" swipe on my phone it closes maps, but either way maps got a backstack so if i do a lot of things in the app it gets laborious..

Basically i want to switch my app back to fullscreen - mutli window in android studio

Any ideas of how to solve this?

case R.id.mapsview:
                    if (DBG) Log.d(TAG, "Show Maps");
                    switchMultiWindow();
                    return true;

@RequiresApi(api = Build.VERSION_CODES.N)
public void switchMultiWindow(){
    if (mapsOpen){
        mapsOpen = false;
        moveTaskToBack(true);

    }else{
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setPackage("com.google.android.apps.maps");
        intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |
                Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        startActivity(intent);
        mapsOpen = true;
    }
}

CodePudding user response:

To close an app that is open in multi-window mode, you can use the finishAndRemoveTask() method. This method will finish the activity and remove it from the recent tasks list.

You can call this method in your switchMultiWindow() method after starting the Google Maps app to close it when the button is clicked again.


@RequiresApi(api = Build.VERSION_CODES.N)
public void switchMultiWindow(){
    if (mapsOpen){
        mapsOpen = false;
        moveTaskToBack(true);
        finishAndRemoveTask();
    }else{
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setPackage("com.google.android.apps.maps");
        intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |
                Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        startActivity(intent);
        mapsOpen = true;
    }
}


It's important to note that finishAndRemoveTask() is only available on API level 21 (Android 5.0 Lollipop) and higher. Also, this method requires the FLAG_ACTIVITY_NEW_TASK flag to be set.

Another approach is to use moveTaskToBack(true) to send the app to the background. This method will move the app to the background, but it will not remove the task from the recent tasks list. This method is available on older versions of Android.

You can also use the Intent.ACTION_CLOSE_SYSTEM_DIALOGS action to close the other app. You can send this action to the package of the app you want to close.

CodePudding user response:

Based on the code you provided, it looks like you are trying to switch between your app and the Google Maps app when a button is clicked.

You are using the moveTaskToBack(true) method to send your app to the background and bring the Google Maps app to the foreground. And when the button is clicked again, you are using the Intent class to launch the Google Maps app in multi-window mode.

However, in this case, the moveTaskToBack(true) method doesn't close the Google Maps app, it just sends your app to the background, so the Google Maps app still remains open on the foreground.

A possible solution to this problem is to use the finish() method to close your app when the button is clicked again. This will close your app and bring the Google Maps app to the foreground. Here is an example of how to use the finish() method:

@RequiresApi(api = Build.VERSION_CODES.N)
public void switchMultiWindow(){
    if (mapsOpen){
        mapsOpen = false;
        finish();
    }else{
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setPackage("com.google.android.apps.maps");
        intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |
                Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        startActivity(intent);
        mapsOpen = true;
    }
}

It's important to note that, like mentioned before, the finish() method will close your app and all activities in the task, so it should be used carefully.

Another alternative could be to minimize your app instead of closing it, in that case you can use Intent.FLAG_ACTIVITY_CLEAR_TOP to minimize the app and bring the Google Maps app to the foreground.

@RequiresApi(api = Build.VERSION_CODES.N)
public void switchMultiWindow(){
    if (mapsOpen){
        mapsOpen = false;
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }else{
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setPackage("com.google.android.apps.maps");
        intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |
                Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        startActivity(intent);
        mapsOpen = true;
    }
}

It's important to keep in mind that the behavior of apps in multi-window mode may vary depending on the version of Android and the specific app, so it's recommended to test this solution on the device you are targeting.

  • Related