updating android activity when a user taps a notification - android

I use this code, inside a notification, so when the user taps the notification the MainActivity starts:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
If the user exits the app with the back button and taps a notification then main activity starts but if the user exit the app with the home button a new MainActivity starts over the first one. How can I detect if the MainActivity is loaded?
Thkx

You should add flags to your Intent to specify this behavior. Something like FLAG_ACTIVITY_CLEAR_TOP may be what you're looking for. This will finish any Activities on top of MainActivity (if any) and bring any existing instance to the foreground. If no instance is available, one will be created. Depending on your launchMode, you'll either get a callback to onNewIntent() (receiving the new Intent you provided) or the activity will be recreated with the new Intent.
Intent mainIntent = new Intent(this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent.getActivity(this, 0, mainIntent, 0);

Related

Android: How to show/skip the Splash Screen as needed when opening an app through a notification?

(Note: My app's targetSdk and compileSdk are set to 29, so I can't take advantage of the new SplashScreen API)
I have a very simple app with two activities:
SplashActivity - Default activity opened with the launcher icon. Shows a splash screen, loads some stuff, and when complete, calls startActivity() to start the Main Activity and then calls finish() to finish itself.
Main Activity - It just posts a notification. When pressing that notification, the app should be brought to the foreground.
What do I mean by "the app should be brought to the foreground"? To be clear:
If MainActivity already exists (either in the background or foreground), open that same instance.
If MainActivity was destroyed (the user previously pressed Back), then open the SplashActivity. The splash activity will redirect you itself to MainActivity.
In fact, this is exactly the default behavior of the launcher icon.
However, I can't get my app to behave this way when the notification is pressed. Either it's always opening the SplashScreen, or it's always opening the MainActivity directly and skipping the Splash Screen.
Ideally, I would like to do this:
Intent intent;
if (MainActivity already exists) {
// Bring that instance of MainActivity to the foreground
intent = new Intent(this, MainActivity.class);
} else {
// No UI activity exists for the app. Start from the Splash Screen.
intent = new Intent(this, SplashActivity.class);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
...
// later
notificationBuilder.setContentIntent(pendingIntent);
But I'm pretty sure I shouldn't be checking for the existence of MainActivity myself. I should be using a combination of Intent Flags and activity launch modes.
in the manifest you should add the following to your main activity
android:launchMode="singleTask"
then my notification looks as follows
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context, MainActivity.NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(text)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(text));
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
Intent goToAppIntent = context.getPackageManager()
.getLaunchIntentForPackage(context.getPackageName())
.setPackage(null)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent piGoToApp = PendingIntent.getActivity(context, 0, goToAppIntent, 0);
mBuilder.addAction(0,"go to app",piGoToApp);
mBuilder.setContentIntent(piGoToApp);
let me know if it helps

Resume background App

Scenario:
App is in the background, main Activity is in onStop state. I receive a specific message, display a notification to the user, he clicks on it and I want to bring the app to the forwground and just resume the main Activity, without starting a new one.
I want the samw effect as you get when you click on a paused app in the recent tasks(it just resumes).
I assume that this has to do with setting the right flags on an Intent, but I couldn't find it.
Is it possible? Thanks.
The Notification just needs to contain a launch Intent to bring your app to the foreground in whatever state it was in:
PackageManager pm = context.getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage(context.getPackageName());
PendingIntent pi = PendingIntent.getActivity(context, requestCode, launchIntent, 0);
Set pi as contentIntent on the Notification.

PendingIntent launches different activity of the app package

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.

Android - Notification Pending Intent - Add to top of activity stack issue

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);

Resume specific activity instead of launching new instance

I am working on a task switcher, and already figured out the way to show list of apps and switch to the app when user presses my notification. My problem is, instead of resuming the app, like launcher does, my app just starts new instance (resets) the activity it's switching to.
Here's the code I'm using to set the Intent:
ComponentName componentName = taskInfo.topActivity;
String packageName = componentName.getPackageName();
/*Setting intent*/
Intent startApp = packageManager.getLaunchIntentForPackage(packageName);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, startApp, PendingIntent.FLAG_UPDATE_CURRENT);
childView.setOnClickPendingIntent(R.id.imageButton2, pendingIntent);
To be clear, I want the user to be able to got back to whatever he was doing in that app instead of starting it anew.

Categories

Resources