Slaiv206
Slaiv206

Reputation: 309

Open activity already open from notification

I create an alarm with notification, but when I open MainActivity through the notification, another MainActivity is open over the previous and if I close MainActivity there is another MainActivity under.

This is the code of my BroadcastReceiver:

    @Override
    public void onReceive(Context context, Intent intent)
    {

        Intent notificationIntent = new Intent(context, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context,
                        0, notificationIntent,
                        PendingIntent.FLAG_CANCEL_CURRENT);

         NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context);
         mBuilder.setContentIntent(contentIntent)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("E' ora di colazione!")
                    .setContentText("Cosa c'è per colazione?")
                    .setAutoCancel(true);


                NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(1, mBuilder.build());


    }   

}

Upvotes: 3

Views: 496

Answers (3)

Slaiv206
Slaiv206

Reputation: 309

I've find a solution using notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

@Override
    public void onReceive(Context context, Intent intent)
    {

        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(context,
                        0, notificationIntent,
                        PendingIntent.FLAG_CANCEL_CURRENT);

         NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context);
         mBuilder.setContentIntent(contentIntent)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("E' ora di colazione!")
                    .setContentText("Cosa c'è per colazione?")
                    .setAutoCancel(true);


                NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(1, mBuilder.build());


    } 

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33248

Add this android:launchMode="singleTask" in your activity tag of Menifest.xml

@Override onNewIntent in your Activity class.

Your class will got new intent in onNewIntent() and proceed whatever you want.

@Override
    protected void onNewIntent(Intent intent) {

        super.onNewIntent(intent);
    }

Upvotes: 1

Napolean
Napolean

Reputation: 5543

Update your intent creation code

add the following flag to the intent FLAG_ACTIVITY_REORDER_TO_FRONT

Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

Upvotes: 0

Related Questions