I have a notification and when I select, it sends a Broadcast
to a BroadcastReceiver
using a PendingIntent
. In the onReceive
I start a new Activity
.
However, if I remove my app from recent apps opened (or the notification sit in the draw for a long time) this scenario occurs:
When I have multiple notifications in the drawer the first one opens great. After tapping on the second one my onCreate()
nor my onResume()
are being called and its as if the startActivity()
is not working at all. If I add the flag Intent.FLAG_ACTIVITY_SINGLE_TOP
then onNewIntent
is being called.
notificationIntent = new Intent();
notificationIntent.setAction(AppConstants.ACTION_ACTIVITY);
notificationIntent.putExtra("key", value);
int requestID = (int) System.currentTimeMillis();
mBuilder.setContentIntent(PendingIntent
.getBroadcast(context, requestID, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
onReceive
Intent intent = new Intent(context, Activity.class);
intent.putExtra("key", value);
//IF I ADD FLAG_ACTIVITY_SINGLE_TOP IT WORKS
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
The startActivity(intent) and startActivityForResult(intent) methods are Asynchronous as they are non-blocking and allow the thread of execution to continue whilst performing their corresponding task as well.
startActivity(i); Then, in your activity, you will need to getExtra as so: Intent intent = getIntent(); String message = intent. getStringExtra("message");
Creating a BroadcastReceiver The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context.
A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent) . Once your code returns from this function, the system considers the object to be finished and no longer active.
Try using flags as follows for the intent.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
And for Pending intent use PendingIntent.FLAG_UPDATE_CURRENT
in order to start activity from BroadcastReciever you should add flag of FLAG_ACTIVITY_NEW_TASK
Intent imap =new Intent(context,MainActivity.class);
imap.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(imap);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With