1

I have a notification in the status bar for my app.The problem with this is that when you press the home button from the app (pushing it to the background) then press on the notification in the list accessed from the status bar, it starts a fresh copy of the activity. All I want to do is resume the app (like when you longpress the home button and press on the app's icon). Is there a way of creating an Intent to do this?

SurenSaluka
  • 1,534
  • 3
  • 18
  • 36
  • 1
    Please refer to http://stackoverflow.com/questions/3305088/how-to-make-notification-intent-resume-rather-than-making-a-new-intent – Lei Guo Oct 09 '14 at 08:52
  • check this : http://stackoverflow.com/questions/3305088/how-to-make-notification-intent-resume-rather-than-making-a-new-intent/39482464#39482464 – TharakaNirmana Sep 14 '16 at 07:52

2 Answers2

1

Declare the launchMode="singleInstance" attribute for your activity in your AndroidManifest. http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

In contrast, "singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.

Dominic
  • 3,353
  • 36
  • 47
  • This is somewhat correct. But this will create issues when user clicks the Home button and clicks on app icon to resume the app. – SurenSaluka Oct 09 '14 at 09:26
0
private void startNotificationOnStatusBar() {
    try {
        Intent notificationIntent = new Intent(this, RadioPlayerActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("Title")
            .setContentIntent(intent)
            .setPriority(2)
            .setContentText("Content text")
            .setAutoCancel(true);
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());
    } catch (Exception e) {
    }
}

This was my code with no AndroidManifest modifications. This is working perfect. When user clicks on the notification it resumes without executing onCreate() method.

SurenSaluka
  • 1,534
  • 3
  • 18
  • 36