3

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


    }   

}
Slaiv206
  • 309
  • 2
  • 14

3 Answers3

1

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);
    }
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • It works but there is a point in my application in which I call a new Intent: `Intent myIntent = new Intent(getActivity(), MainActivity.class); getActivity().startActivity(myIntent); getActivity().finish();` in this point my app crash – Slaiv206 Apr 20 '15 at 07:36
0

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);
Napolean
  • 5,303
  • 2
  • 29
  • 35
0

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


    } 
Slaiv206
  • 309
  • 2
  • 14