Search code examples
androidandroid-intentandroid-notificationsandroid-notification-bar

Android Notification intent destroyes activity


I'm trying to use notifications in my app, but I've got a problem using following code:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification)
        .setContentTitle("title")
        .setContentText("message");
        builder.setOngoing(true);
        Intent resultIntent = new Intent(this,StartActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(StartActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
        builder.setContentIntent(resultPendingIntent);
        notificationManager.notify(1, builder.build());

When I return to homescreen and click the notification in the status bar StartActivity gets destroyed and created again. But, I just want the Activity to show again like clicking the launcher icon would do in this ,moment. How can I change that?

regards


Solution

  • Following worked for me:

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification)
            .setContentTitle("title")
            .setContentText("message");
            builder.setOngoing(true);
            Intent resultIntent = new Intent(this,NotificationActivity.class);///!!!!
            resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
            builder.setContentIntent(resultPendingIntent);
            notificationManager.notify(1, builder.build());
    

    NotificationActivity is special class, which finishs immediately in onCreate() and brings the task of the app to foreground. Source: Notification to restore a task rather than a specific activity?