8

I need to access location from several non-UI components in my app. Since the app may not have the required permission, I implemented a solution outlined in the answers to How to request permissions from a service in Android Marshmallow: if the app doesn't have permission, it displays a notification. Tapping that notification will take the user to an empty Activity, which displays the runtime permissions dialog on top.

However, I have the following issue now: If the user dismisses that dialog without making a choice (e.g. by tapping Home) and later brings up any other activity of my app, the runtime permission dialog reappears instead of the desired activity.

Here's my code for the notification:

    Intent permIntent = new Intent(context, PermissionRequestActivity.class);
    permIntent.putExtra(Const.KEY_RESULT_RECEIVER, resultReceiver);
    permIntent.putExtra(Const.KEY_PERMISSIONS, permissions);
    permIntent.putExtra(Const.KEY_REQUEST_CODE, requestCode);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(PermissionRequestActivity.class);
    stackBuilder.addNextIntent(permIntent);

    PendingIntent permPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                    );

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    .setSmallIcon(notificationIcon)
    .setContentTitle(notificationTitle)
    .setContentText(notificationText)
    .setOngoing(true)
    .setAutoCancel(true)
    .setWhen(0)
    .setContentIntent(permPendingIntent)
    .setStyle(null);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(requestCode, builder.build());

Is there a way I can abort the runtime permissions dialog when its Activity is stopped?

Community
  • 1
  • 1
user149408
  • 5,385
  • 4
  • 33
  • 69
  • 1
    Are you using `FLAG_ACTIVITY_NEW_TASK` in your `Intent` for the `PendingIntent` for the `Notification`? Your permission-from-notification UI flow should be in a separate task from your main app UI flow. – CommonsWare Jul 09 '16 at 14:59
  • @CommonsWare where would I specify that? `Intent#addFlag()`? (I've added my current code above.) – user149408 Jul 09 '16 at 15:20
  • @CommonsWare I've dropped the `TaskStackBuilder` stuff and replaced it with `PendingIntent#getActivity()`. Now the runtime permissions dialog displays on top of the activity. Same if I add `permIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);`. – user149408 Jul 09 '16 at 15:25
  • Once you get `PermissionRequestActivity` and the activity that is the permission-request dialog in its own task, [try `finishAndRemoveTask()`](https://developer.android.com/reference/android/app/Activity.html#finishAndRemoveTask()) in `onStop()` of `PermissionRequestActivity`. – CommonsWare Jul 09 '16 at 16:01
  • @CommonsWare Still no joy. The only difference it makes is that `finishAndRemoveTask()`, if called after `super.onStop()`, will cause the notification's runtime permission dialog to display on top of the one created by the app's main `Activity`, rather than below it. – user149408 Jul 09 '16 at 16:15
  • Phooey. Perhaps the problem is that the permission dialog is from another "app" (really, the OS). Ideally, it would close itself if the user abandons it. I don't have any other ideas of how you can convince that activity to go away -- sorry! – CommonsWare Jul 09 '16 at 16:18

0 Answers0