diff --git a/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java b/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java index 60aeed07..4a15649d 100644 --- a/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java @@ -357,6 +357,11 @@ public class PermissionUtils { intent.addCategory("android.intent.category.DEFAULT"); intent.setData(Uri.parse("package:" + context.getPackageName())); + // Flag must not be passed for activity contexts, otherwise onActivityResult() will not be called with permission grant result. + // Flag must be passed for non-activity contexts like services, otherwise "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag" exception will be raised. + if (!(context instanceof Activity)) + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + Error error; if (requestCode >=0) error = ActivityUtils.startActivityForResult(context, requestCode, intent, true, false); @@ -474,6 +479,11 @@ public class PermissionUtils { Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); intent.setData(Uri.parse("package:" + context.getPackageName())); + // Flag must not be passed for activity contexts, otherwise onActivityResult() will not be called with permission grant result. + // Flag must be passed for non-activity contexts like services, otherwise "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag" exception will be raised. + if (!(context instanceof Activity)) + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + if (requestCode >=0) return ActivityUtils.startActivityForResult(context, requestCode, intent); else @@ -549,6 +559,11 @@ public class PermissionUtils { Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + context.getPackageName())); + // Flag must not be passed for activity contexts, otherwise onActivityResult() will not be called with permission grant result. + // Flag must be passed for non-activity contexts like services, otherwise "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag" exception will be raised. + if (!(context instanceof Activity)) + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + if (requestCode >=0) return ActivityUtils.startActivityForResult(context, requestCode, intent); else