Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android PendingIntent take you to an already existing activity?

Tags:

Wasn't really sure how to search for this...

I have a the following which is called whenever a job is added or removed from my queue to put a notification in the status bar:

private void showNotification() {     int jobsize = mJobQueue.size();     int icon = (jobsize == 0) ?          android.R.drawable.stat_sys_upload_done :          android.R.drawable.stat_sys_upload;     Notification notification =          new Notification(icon, "Test", System.currentTimeMillis());     Intent intent = new Intent(this, FileManagerActivity.class);     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);      notification.flags =          (Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL);     notification.setLatestEventInfo(this,          "Uploading to our servers",         getString((jobsize > 0) ?              R.string.notification_active_transfers :              R.string.notification_no_transfers),         pendingIntent);      mNotifyManager.notify(NOTIFICATION, notification); } 

As it is now the behavior is this:

  • if the user logs out and hits the notification, it will still open a new FileManagerActivity (ops!) I could get around this by starting at my authentication activity and passing the intent up my stack in a natural order, its when the application is already running is where I have difficulties.

  • if the user already has the FileManagerActivity open clicking the notification will put a second instance over it. In this case, I want the currently running FileManagerActivity to recieve focus instead of launching a new instance.

How could I get the correct behavior?

like image 574
Tom Fobear Avatar asked Mar 28 '11 21:03

Tom Fobear


People also ask

What is the pending intent in android?

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it.

What is requestCode in PendingIntent?

1- requestCode is used to get the same pending intent later on (for cancelling etc) 2- Yes, they will get override as long as your specify the same Receiver to your Intent that you specify on your PendingIntent.

How do I start a pending intent?

Create an Intent that starts the Activity . Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK . Create a PendingIntent by calling getActivity() .

What is pending intent in notification?

Android PendingIntent In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.


2 Answers

I've done this before by setting my Activity to use the launch mode 'singleTop' in the Application Manifest. It will achieve the desired function, using the existing activity if one exists. In this case, onNewIntent will be called in your activity.

You'll need to check in your FileManagerActivity for authentication and start a new activity as appropriate if the user is not logged in.

like image 159
David Snabel-Caunt Avatar answered Oct 19 '22 10:10

David Snabel-Caunt


I think Worked when added these:

intent.setAction(Long.toString(System.currentTimeMillis()));

PendingIntent.FLAG_UPDATE_CUR

Intent intent = new Intent(context, MyOwnActivity.class); intent.putExtra("foo_bar_extra_key", "foo_bar_extra_value"); intent.setAction(Long.toString(System.currentTimeMillis())); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT); 
like image 42
Ahmad Ronagh Avatar answered Oct 19 '22 09:10

Ahmad Ronagh