Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());


    }   

}
like image 433
Slaiv206 Avatar asked Jan 21 '26 07:01

Slaiv206


1 Answers

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);
    }
like image 138
Niranj Patel Avatar answered Jan 23 '26 19:01

Niranj Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!