In Android, I defined an activity ExampleActivity.
When my application was launched, an instance of this A-Activity was created, say it is A.
When user clicked a button in A, another instance of B-Activity, B was created. Now the task stack is B-A, with B at the top. Then, user clicked a button on B, another instance of C-Activity, and C was created. Now the task stack is C-B-A, with C at the top.
Now, when user click a button on C, I want the application to bring A to the foreground, i.e. make A to be at the top of task stack, A-C-B.
How can I write the code to make it happen?
You can try this FLAG_ACTIVITY_REORDER_TO_FRONT (the document describes exactly what you want to)
The best way I found to do this was to use the same intent as the Android home screen uses - the app Launcher.
For example:
Intent i = new Intent(this, MyMainActivity.class);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
This way, whatever activity in my package was most recently used by the user is brought back to the front again. I found this useful in using my service's PendingIntent to get the user back to my app.
Here is a code-example of how you can do it:
Intent intent = getIntent(getApplicationContext(), A.class)
This will make sure that you only have one instance of an activity on the stack.
private static Intent getIntent(Context context, Class<?> cls) {
Intent intent = new Intent(context, cls);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
return intent;
}
FLAG_ACTIVITY_REORDER_TO_FRONT:
If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.
Intent i = new Intent(context, AActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
I think a combination of Intent flags should do the trick. In particular, Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK.
Add these flags to your intent before calling startActvity.
i.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Note Your homeactivity launchmode should be single_task
In general I think this method of activity management is not recommended. The problem with reactivating an activity two Steps down in The Stack is that this activity has likely been killed. My advice into remember the state of your activities and launch them with startActivity ()
I'm sure you've Seen this page but for your convenience this link
If you want to bring an activity to the top of the stack when clicking on a Notification then you may need to do the following to make the FLAG_ACTIVITY_REORDER_TO_FRONT work:
The solution for me for this was to make a broadcast receiver that listens to broadcast actions that the notification triggers. So basically:
Notification triggers a broadcast action with an extra the name of the activity to launch.
Broadcast receiver catches this when the notification is clicked, then creates an intent to launch that activity using the FLAG_ACTIVITY_REORDER_TO_FRONT flag
Activity is brought to the top of activity stack, no duplicates.
If you use adb am cmd for start,you should use cmd like below
adb shell am start -n com.xxx.xxx/com.xxx.Activity --activity-reorder-to-front
adb intent argument
adb intend -f argument descript
if you are using the "Google Cloud Message" to receive push notifications with "PendingIntent" class, the following code displays the notification in the action bar only.
Clicking the notification no activity will be created, the last active activity is restored retaining current state without problems.
Intent notificationIntent = new Intent(this, ActBase.class);
**notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);**
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Localtaxi")
.setVibrate(vibrate)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Ciao!
Related
(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
Per title, it doesn't always start the activity.
There's no error in output log, it just says
06-01 16:46:36.924: I/ActivityManager(370): START u0 {flg=0x10000000
cmp=com.myapp/md527315440e30c82eb86ffbe7caee6cb98.MyView
bnds=[96,712][1056,840] (has extras)} from pid -1
What I mean by "not always" is this:
I start app, it shows the main screen. Notification is received, I tap on notification, activity is run. App navigates to a different activity, and this activity is closed (finish() is called).
Next, I tap on back button until app is closed. Notification is received, I tap on notification, activity is run. App navigates to a different activity, and this activity is closed (finish() is called).
Next, keeping app in same state (no navigation). Notification is received, I tap on notification, activity is NOT run.
Here's the code which is adding a notification:
void CreateNotification (Context context, PushNotification pn)
{
var builder = new NotificationCompat.Builder (context)
.SetContentTitle (pn.Title)
.SetContentText (pn.Body)
.SetSmallIcon (Resource.Drawable.launcher)
.SetLargeIcon (Android.Graphics.BitmapFactory.DecodeResource (context.Resources, Resource.Drawable.launcher))
.SetSound (Android.Provider.Settings.System.DefaultNotificationUri)
.SetAutoCancel (true);
Android.Support.V4.App.TaskStackBuilder stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(context);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(LoginView)));
stackBuilder.AddNextIntent(GetIntent());
PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
builder.SetContentIntent(resultPendingIntent);
var notificationManager = context.GetSystemService (Context.NotificationService) as NotificationManager;
notificationManager.Notify(1, builder.Build());
}
static Intent GetIntent ()
{
// returns an intent
}
I saw other questions with the same issue but no solution works.
If someone can give some hints or ideas as why this might not work.
Check out the "Setting up a regular activity PendingIntent" section of the android notifications. You are not following the guidelines laid out in the example. Particularly, you don't do the following:
Create a back stack based on the Intent that starts the Activity:
Create the Intent to start the Activity. Create a stack builder by
calling TaskStackBuilder.create(). Add the back stack to the stack
builder by calling addParentStack(). For each Activity in the
hierarchy you've defined in the manifest, the back stack contains an
Intent object that starts the Activity. This method also adds flags
that start the stack in a fresh task. Note: Although the argument to
addParentStack() is a reference to the started Activity, the method
call doesn't add the Intent that starts the Activity. Instead, that's
taken care of in the next step.
Add the Intent that starts the Activity from the notification, by
calling addNextIntent(). Pass the Intent you created in the first step
as the argument to addNextIntent(). If you need to, add arguments to
Intent objects on the stack by calling
TaskStackBuilder.editIntentAt(). This is sometimes necessary to ensure
that the target Activity displays meaningful data when the user
navigates to it using Back. Get a PendingIntent for this back stack by
calling getPendingIntent(). You can then use this PendingIntent as the
argument to setContentIntent().
Sorry about the formatting. I'm unsure how to preserve the original from the site.
I've been doing a lot of research on this and I've hit a wall. I've read what seems like all of the material on this topic (inluding this, this, and this as well as the docs) and nothing is helping me in what I want to do.
Basically, in the case that the app is open on the user's phone, I just want a notification to redirect the user to the already-existing Activity in my app. I use a pattern like this:
private PendingIntent buildPendingIntentForNotification(String type) {
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setAction(Intent.ACTION_VIEW);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);// have tried without this and with Intent.FLAG_ACTIVITY_CLEAR_TOP
resultIntent.putExtra(CommonUtils.NOTIFICATION_TYPE, type);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
return resultPendingIntent;
}
I've also tried declaring all 4 types of launchModes in the Android Manifest within the activity tag. No matter what I do, clicking on the notification (even if the app is the active foreground app on the screen and MainActivity is the active activity) always seems to restart the activity from onCreate. From what I can see during debugging, onDestroy is not called at any time between getting the notification and clicking on it. However, after the notification is clicked, onCreate is called and THEN onDestroy is called even though my activity is not being destroyed, which is very strange. I'm hoping someone can help me make sense of this because all of the launchMode and Intent.setFlags suggestions are not working for me. Thanks a lot!
Just for info's sake, here's the code I used to fix my problem (with credit to David's solution):
private PendingIntent buildPendingIntentForNotification(String type) {
Intent resultIntent = new Intent(this, MainActivity.class);
//resultIntent.setAction(Intent.ACTION_VIEW);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resultIntent.putExtra(CommonUtils.NOTIFICATION_TYPE, type);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
return resultPendingIntent;
}
There are 2 ways of doing this:
Notification to restore a task rather than a specific activity?
Resume application and stack from notification
However, you may also be seeing this nasty Android bug.
You cannot control and cannot be sure about the state of your activities on the Android, meaning (in your context) you can only start an activity that has been paused not one that has been destroyed. The OS decides which activities will keep paused and which will destroy and you have no control over that. When your activity leaves the foreground successive callbacks are being invoked at undefined times and to the OS's
What you can simply do is to save your activity's instance state in the onPause() method (which we know for sure that will be called as soon as the activity leaves the foreground) and the on onCreate() you can restore the activity with the data as it previously was.
Note: If you have for example Activity A and activity B and you start activity B from within activity A, then on activity B's onCreate() method you can getIntent().getExtras()
My android application generates a notification. This notification is definid in the following way:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_alarms)
.setContentTitle("title")
.setContentText("example text");
Intent resultIntent = new Intent(this, MyActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
So when I open the notification drawer and click on the notification area, the actvity MyActivity starts. But, If another instance of the MyActivty is already running, I obtain that two MyActivty instances will running at the same time, while I want that always just one instance at time, runs in my application.
I have tried using
android:launchMode="singleTop"
android:clearTaskOnLaunch="true"
but the result is the same.
How can I achieve this task?
Ultimately, you probably need to think about using Fragments, instead of Activities for the kind of UI interaction you are wanting (and really the way the NavDrawer is designed to work - and they way the official examples implement it).
You can use different intent flags to bring an old Activity back to the front, but this is really not the intended behavior you ultimately need (which is Fragment related).
Ex Intent Flags
Intent intent = new Intent(this, ActivityExamlpe.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
There are a lot of questions/answers about how to start an application from within your application in Android. But those solutions do not produce the same flow as if an icon was tapped in Android launcher.
For example, I do this (this is used with notifications):
intent = context.getPackageManager().getLaunchIntentForPackage("com.test.startup");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pendingIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Then when I tap on notification the app is started, however, it is started somewhat differently than when I tap the icon in the App drawer. Specifically: with this approach my main Activity is always created (i.e. onCreate() then onResume() is called). However, if application was already started and then put in background, then starting it from Launcher will only cause onResume() of currently shown activity to be called (not onCreate() on the main one). Is there a way to trigger the same resume flow programmatically from within my app?
To summarize the task: when user taps on notification I need my app to be either started (if it's not already), or brought to the foreground in its current state (if it's in background) and have some data passed to it. The app will then take care of handling/rendering that data.
Your app is behaving the way it supposed to. Even if you try the launch the app from App drawer it will call the same callback. You have to understand the lifecycle. As your activity is in the background onCreate will not get called. But for the handling the data from the notification intent you should utilize callback method OnNewIntent() in activity. You should override this method and extract the data the from the new intent and should update UI. After onNewIntent onresume will be called.
I hope this solves your problem.
Here is my onPause code which works the way you expected i.e when user clicks on the notification it doesnt call onCreate again:
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(getApplicationContext(), PlayerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(getBaseContext(), 0, intent,0);
NotificationCompat.Builder noti =
new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_media_play)
.setContentTitle("Nepali Music And more")
.setContentText("Playing");
noti.setContentIntent(pIntent);
noti.setAutoCancel(true);
noti.setOngoing(true);
Notification notification = noti.getNotification();
notificationManager.notify(1, notification);
Focus mainly on the intent flags
You want to use the intent flags Intent.FLAG_ACTIVITY_CLEAR_TOP to find your activity and clear the stack above it. You also need the Intent.FLAG_ACTIVITY_SINGLE_TOP flag to prevent your activity from being recreated (to resume).
The Intent.FLAG_ACTIVITY_SINGLE_TOP is necessary since by default, the launch mode is "standard" which lets you create multiple instances of your activity. If you were to set your launch mode to SingleTop, then this flag own't be necessary