Search code examples
androidandroid-intentbroadcastreceiverandroid-notifications

Resume background App


Scenario: App is in the background, main Activity is in onStop state. I receive a specific message, display a notification to the user, he clicks on it and I want to bring the app to the forwground and just resume the main Activity, without starting a new one. I want the samw effect as you get when you click on a paused app in the recent tasks(it just resumes).

I assume that this has to do with setting the right flags on an Intent, but I couldn't find it. Is it possible? Thanks.


Solution

  • The Notification just needs to contain a launch Intent to bring your app to the foreground in whatever state it was in:

    PackageManager pm = context.getPackageManager();
    Intent launchIntent = pm.getLaunchIntentForPackage(context.getPackageName());
    PendingIntent pi = PendingIntent.getActivity(context, requestCode, launchIntent, 0);
    

    Set pi as contentIntent on the Notification.