I wrote my notification intent like this(the snippet below). I have Activity A,B and C running. While running C, I pressed the home screen and soon received a notification. I pressed on the notification icon, hoping restart the app from Activity A but unfortunately it doesn't. What this code current do is create a new Activity A on top of the stack.... So I am stuck with the following running activity(or stack):A,B,C,A
So my main question is, how can I clean up the Activity stack so that only Activity A is on the stack when the notification icon is clicked?
Any tips or comments would appreciated.
Intent notificationIntent = new Intent(context,
A.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(appContext, contentTitle,
contentText, contentIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(ns);
mNotificationManager.notify(1, notification);
Set launchMode="singleTask" for the activity A in the manifest. In this case activity A will be the only one running after you go back to it from notification.
However when launch mode is singleTask or singleInstance your app is going to behave the same way when you resume your app from the background. It's not going to be possible to resume to activity B or C.
This question may help you either with FLAG_ACTIVITY_CLEAR_TOP or using startActivityForResult.
Related
I'm developing notifications in my app, and I'm having some issue with the pending intent that is driving me crazy.
Normal flow: My app has the launcher activity (Activity A, singleTop) which shows a Splash and then launches Activity B (singleTop too).
Notification:
When app is in background, I show a notification on the notification bar, which opens the launcher activity of my app when clicked, through a PendingIntent. This PendingIntent addresses to Activity A (singleTop). But in this scenario, instead of open Activity A, it brings to foreground the Activity B, but without calling onNewIntent() (onResume() is being called instead), so I can't retrieve the extras of the notification Intent and show the information, because this Activity B.getIntent() retrieves the old intent which opened the activity the first time.
Can any of you bring me some light on this issue, please?
This is how I set up the PendingIntent:
Intent notificationIntent = new Intent(context, SplashActivity.class);
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_TYPE, "Notification");
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_MESSAGE, "Message");
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_TITLE, "title");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Edited:
Following the answer given by #Woodi_333, the code for creating the pending intent is as follow.
Intent notificationIntent = new Intent(context, SplashActivity.class);
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_TYPE, "Notification");
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_MESSAGE, "Message");
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_TITLE, "title");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Some code would help but I can take a guess at what is causing this.
So it sounds like the Activity Stack before clicking the PendingIntent is A,B. When opening the PendingIntent, it closes B because of FLAG_ACTIVITY_CLEAR_TOP but leaves activity A alone so it remains running.
So onNewIntent won't run because A is still running, and the FLAG_ACTIVITY_SINGLE_TOP flag won't cause it to run as when the intent is fired, it is not on the top of the history stack.
You might want to combine it with FLAG_ACTIVITY_NEW_TASK as suggested in Documentation for FLAG_ACTIVITY_CLEAR_TOP
I'd also recommend looking at the flags you defined in the manifest to see if they are interfering with the flags of the Pending Intent, or add them to the activities so every time you launch them, they are obeying the same rules.
I have an app that has an activity, say A. and I put a notification to the notification bar that starts A. That is, when user select the notification, A activity will be started. But when user presses back button, the system will go to Home screen, instead of the previous activity(maybe of another app). How can i just let the system finish A activity and go back to the previous activity instead of home screen?
My code for creating the notification:
Notification.Builder mBuilder = new Notification.Builder(this).setSmallIcon(R.drawable.icon).setContentText("title");
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
I seem to find the answer, add the following two lines:
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
and remove this line:
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
But actually I don't understand why.
Would anyone explain it or give a better answer?
I'm a bit confused with pending intents in the notification builder. I've got a MainActivity activity and a MessageList activity. I have a service to show a notification when a new message in found, and I want it to be that if the user presses the notification it opens to the MessageList activity but when they press back they will return to the activity they were in.
Essentially I want to add MessageList activity to the top of the activity stack when they press the notification without modifying the current activity stack.
Thank you
Okay, so I got it to work with some old code I wrote a while back. This does what I wanted -
Intent notificationIntent = new Intent(this, AlertListActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
mBuilder.setContentIntent(contentIntent);
I currently have a notification that displays in the notification bar and when the user clicks it it should bring them to the root Activity of my application.
I have used android:finishOnTaskLaunch ="true" & android:clearTaskOnLaunch="true" in my manifest to achieve this when the user presses the app icon from the apps screen or the home screen. The user is always taken to the root activity as expected.
However when I use the following code from my notification the user gets taken to the last screen they were on rather than the root activity which is StartActivity.
Does anyone know what I'm doing wrong?
Intent notificationIntent = new Intent(this, StartActivity.class);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), getString(R.string.notification_app_name), notificationText, contentIntent);
mNotificationManager.notify(1, notification);
Adding these flags helps however it still doesn't work after a restart:
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
When I restart my application I set a service running which pops up the notification and when I do this the notification reverts back to the old undesired behavior .
SOLVED:
Solved the issue by adding this flag to the contentIntent of my notification - Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED.
Here is an link with some excellent information on the area Google dev group
try this 2 properties
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1/I don't want to notify user if this one is already running my app in foreground,
is it possible before create nofification to check if my app is not in front ?
2/if app is in background, is it possible to bring last state in front ?
I know that Os can destroy some Activity, but is it possible to restore last state, don't start a new Intent?
because if i start a new Intent and i push back, old Intent appear it's not very beautiful to have 2 identical intent launch.
Thanks
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Intent notificationIntent = new Intent(this, z_finish.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
is it possible before create nofification to check if my app is not in front ?
Your activities will have to tell the service when they are being paused and resumed; the service can then decide whether to display the Notification or not.
if app is in background, is it
possible to bring last state in front
?
I am not quite certain what "last state" is. Try FLAG_ACTIVITY_CLEAR_TOP in your Intent.
if your app is in singletask mode then the last state will be saved even if you relaunch it