Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startActivity not working when calling from BroadcastReceiver

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);
like image 601
user1163234 Avatar asked Feb 11 '15 21:02

user1163234


People also ask

Is startActivity asynchronous?

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.

How pass data from BroadcastReceiver to activity in Android?

startActivity(i); Then, in your activity, you will need to getExtra as so: Intent intent = getIntent(); String message = intent. getStringExtra("message");

What is the role of the onReceive () method in the BroadcastReceiver?

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.

What is the life cycle of BroadcastReceiver?

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.


2 Answers

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

like image 64
Roadblock Avatar answered Oct 24 '22 01:10

Roadblock


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);
like image 22
Hussein Alrubaye Avatar answered Oct 24 '22 01:10

Hussein Alrubaye