PendingIntent resume activity - android

I have an Android activity that places a Notification in the status bar. Selecting the notification will launch my main activity. The problem is that this PendingIntent always starts a new instance of my activity "MyappMain". So when i start my app by clicking on the notification, i get an new instance of the MyappMain and then i must quit/finish them all one by one (if I clicked multiple times on the notification).
Intent notificationIntent = new Intent(this, MyappMain.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(HELLO_ID, notification);

Please add finish(); before you start a new Intent, this way your current activity will be finished and new one will be started.

You have to set the "FLAG_ACTIVITY_REORDER_TO_FRONT" flag.
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

Related

Launch An Activity Afresh(Don't Retain State) On A Notification Click

I want to launch an Activity(Afresh, Without any previously retained state) on a notification click. IF this Activity is already in the stack, bring this to top but without any previous state.
So Far i have tried setting Flags to Passed Intent.
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|INTENT.FLAG_ACTIVITY_SINGLE_TOP)
But They Don't serve the purpose. Any suggestions how i could achieve this.
If you remove |Intent.FLAG_ACTIVITY_SINGLE_TOP from your launch this should restart the activity.
Specifying |Intent.FLAG_ACTIVITY_SINGLE_TOP in the launch flags causes the existing instance to be reused. If you remove it the existing instance will be finished and a new instance will be launched.
Here You Go.
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
to open application in anystate.
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);

My service resets internal variables when opening ongoing notificiation

I have a setup where my activity starts a service and sends values through the intent that control how the service acts. These variables can change through the life of the service, and the service pushes updates back to the activity UI. If I exit the activity and relaunch it, it will request an update from the service to get the current values. This works fine if I re-launch the activity via the home screen, or if I use the recent apps menu. However if I launch the activity via the run in foreground ongoing notification I have set for the service, then the service re-sets itself to the original values when it was started.
Here's my run in foreground code, I don't think there is anything else that would have an affect on this problem.
//Create notification and set to run in forground
Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.app_name),
"Running Noise Based", pendingIntent);
startForeground(ONGOING_NOTIIFICATION, notification);
Update 3:
Adding the flags Intent.FLAG_ACTIVITY_NEW_TASK, Intent.FLAG_ACTIVITY_CLEAR_TOP, and Intent.FLAG_ACTIVITY_SINGLE_TOP to the notification intent fixed the problem. I have no idea why these fixed it. If anyone can tell me why this happens please do. Here's the new code that works.
//Create notification and set to run in forground
Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.app_name),
"Running Noise Based", pendingIntent);
startForeground(ONGOING_NOTIIFICATION, notification);

How to listen onClick event for notififation bar on Android?

Well, I have a simple notification. But when I tap it in Notification bar, I just get opened my app. So how can I know that my app was opened by notification tap or how can I open a specific Activity after tapping?
There is no onClick event on a notification. It is merely launching a prepackaged PendingIntent that is attached to THAT specific notification object.
I think you have this code in Statusbar Notification. When you click the Notification Item, the following Intent is called.
Context context = ctx.getApplicationContext();
Intent notificationIntent = new Intent(ctx, ctx.getClass());
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
If you want specific Activity to be opened, call the particular Class in the above Intent

Android notification click

I am running a service which is popping notification on getting a new task. If I close my app it's running in the background and pop up a notification. What I would like to do is, when I click on that notification, open the app. Can I do that ? How ?
You can do this by adding an intent to your notification:
Intent notificationIntent = new Intent(context, YourAppActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, "headline", "body msg", contentIntent);
notifcationMgr.notify(id, notification);
Just replace YourAppActivity with the activity you want to launch.

How can I cancel a notification when the user clicks on it?

I would like to create a notification that clears automatically when the user presses it and has no other behaviuour. The notification below works fine but when i press on it it takes me to the activity "MyActivity" (even having to define an intent seems a bit unecessary when I don't want to use it...)
Using FLAG_AUTO_CANCEL doesn't seem to have any effect at all.
Update: Sorry, I have found the FLAG_AUTO CANCEL does work, that is the notification is cleared from the status bar. I guess I am really tring to write an intent that does nothing (or completely delete the intent).
Code...
Notification notification = new Notification(R.drawable.success, res.getString(R.string.messages_sent), System.currentTimeMillis());
//Define the expanded message and intent
Context context = getApplicationContext();
CharSequence contentTitle = res.getString("My content title");
Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0 );
notification.setLatestEventInfo(context, contentTitle, mStrSuccessMessage, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//Pass the notification to the notification manager
mNotificationManager.notify(1, notification);
Use an empty intent without an actual action.
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(), 0);
I don't think you can. You can have MyActivity exit immediately though: call finish() from onCreate().

Categories

Resources