2

I've read multiple questions and answers surrounding this issue, however I cannot get any of them to work for me.

I have a notification that on click I would like to bring the application to the front and resume rather than close and restart.

This is my notification code

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("example")
            .setContentText("example");
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, MainActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int mId = 0;
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());

And in my manifest file I have

        android:launchMode="singleTop"

Can anyone see what is going wrong? I get no errors, although the notification refuses to resume the app and instead restarts it.

rossd
  • 229
  • 1
  • 3
  • 14
  • http://stackoverflow.com/questions/2232238/how-to-bring-an-activity-to-foreground-top-of-stack – weaknespase Feb 24 '14 at 18:13
  • **resultIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);** Still causing app to restart. – rossd Feb 24 '14 at 18:26
  • Maybe, activity you trying to bring to front already destroyed? – weaknespase Feb 24 '14 at 18:32
  • The application plays sounds on button press. Testing these methods I've been playing a sound, minimising the app, then pulling down the draw and selecting the notification. When the notification is selected the music stops and the app restarts. – rossd Feb 24 '14 at 18:36
  • Try `singleTask` or `singleInstance`, maybe it will help. And i think testing with sound could be incorrect. When i watched activity lifecycle, i used log and lifecycle callbacks to print messages. – weaknespase Feb 24 '14 at 18:47

3 Answers3

5

Fixed by using this:

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Sound Asleep")
            .setContentText("Click to play and stop sounds");
    // Creates an explicit intent for an Activity in your app
    final Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.

    // Adds the back stack for the Intent (but not the Intent itself)
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int mId = 0;
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());
}

In Manifest

android:launchMode="singleTop"
rossd
  • 229
  • 1
  • 3
  • 14
1

I had some similar issue recently change your intent flags with this flags:

resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Edit: and remove that launchmode from your manifest.

hope it help

murielK
  • 1,000
  • 1
  • 10
  • 21
  • Tried and the app is still reloading, it's very confusing for me as to find out why. – rossd Feb 24 '14 at 18:25
  • Instead of using a stackbuilder just create a PendingIntent resultPendingIntent = PendingIntent.getActivities(context,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT) since you set flags on your resultIntent i dont think its really necessary – murielK Feb 24 '14 at 18:32
  • Removing those and having - PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT); is still restarting the app. It seems nothing will fix my problem. – rossd Feb 24 '14 at 19:19
0

Use below code, may it will help

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Sound Asleep")
        .setContentText("Click to play and stop sounds");
// Creates an explicit intent for an Activity in your app
final Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.

// Adds the back stack for the Intent (but not the Intent itself)
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_ACTIVITY_REORDER_TO_FRONT);

mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 0;
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
Solution
  • 604
  • 4
  • 10