Search code examples
androidandroid-activityandroid-preferencesandroid-permissions

onActivityResult() is called to early for Settings.ACTION_MANAGE_OVERLAY_PERMISSION


I want to request the Overlay permission for my app to draw on top of other apps. For this I use ACTION_MANAGE_OVERLAY_PERMISSION

    Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivityForResult(intent, 1234);

This works well and shows the appropriate setting screen but onActivityResult is called when the setting activity is first shown and not when destroyed so I never get the permission in onActivityResult. Do I miss something ?


Solution

  • This is because you are starting a new task. From the documentation

    [...] if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result.

    So it should work if you remove

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);