I have an odd issue with my application.
I start the application and the activity shows ok.
I then press the home key so that the activity goes into the background. I can see the onPause() method being called.
The application's service then creates a notification which shows on the status bar.
I then click on the notification and the activity is shown and I see that the onResume() method is called.
I then press the home key and the activity goes into the background. I can see the onPause() method being called.
If I now start the application by clicking on the applications icon I see that a new instance of the activity is created rather than using the paused instance.
If I press the home key again the new activity goes into the background.
Starting the application by clicking on the applications icon I see another new instance of the activity is created.
Pressing the back button at the point destroys each activity in return.
What I want to happen is that a single instance of the activity be used.
Any ideas?
Just use the same intent filters as android uses when launches the app:
final Intent notificationIntent = new Intent(context, MessageListActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
As the intent you created to open your activity from notification bar is the same as android used for launching your app, the previously opened activity will be shown instead of creating a new one.
You should look at the different launch modes for Android activities. this should help. Launch modes can be set in the androidmanifest.xml file. I think your solution would be to use the 'singleTop' launch mode.
Related
I have an activity in my application that does a process and when finished, launches an activity with a normal intent:
Intent intent2 = new Intent();
intent2.setClass(anActivity.this, JustAnotherActivity.class);
startActivity(intent2);
finish();
The problem is that when you are doing the process, if I press the home button and take it to the background, when it finishes and calls the new activity, even if it is in the home of the phone, this new activity jumps to the foreground.
I am looking for the activity to be launched, but if the user is outside the application, it does not come to the foreground.
I guess the use of a FLAG is necessary, but the ones I have tried have not worked.
sorry for my bad english
Thanks.
what if you take care of ActivityLifecycle (OnPause, OnStop).
You can add a flag when the activity goes "OnPause/OnStop" depending what you want. And them perform the intent just if the activity is not "Pause/Stop".
Activity Lifecycle
I have an activity which is called if the app receives a push notification. The activity is started with FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP. The activity, let's call it 'A' shows UI and finishes after a while. In this point have a problem with activity stack.
Scenario:
The app is in the background with another activity 'B'
Then the app receives a push notification and starts Activity A.
After related things done, the app finishes Activity A
Then returns to Activity B and stays in the foreground even the app was in the background before the push notification is received.
After debugging, I figured out that the system calls onResume method of Activity B after finishing Activity A.
How can I do the app keep staying in background if the app started from background? Should I change intent flags of the activity A?
In your case you can achieve this in two ways
1- From manifest file with activity tag android:noHistory="true"
2- From code when you are staring the activity set flags like below
Intent mIntent = new Intent(context, Youractivity.class);
mIntent.setFlags(mIntent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(mIntent);
For more information checkout developers link
One other thing you can do is instead of this.finish() in notificationActivity is to use this.finishAffinity();. This will close the app instead coming to foreground.
This question already has answers here:
Resume application and stack from notification
(8 answers)
Closed 6 years ago.
Right now I'm using a PendingIntent to launch my desired Activity from the Notification. But I want to know if it's possible to reuse the current app instance.
For example:
Launch app, the Launcher Activity is called HomeActivity
Navigate to SecondActivity
Press the home button
Click on the notification from the app and resume SecondActivity
But if the user didn't navigate to the SecondActivity i want to open the HomeActivity when I click on the Notification. I would be glad for any help regarding this problem :)
But I want to know if it's possible to reuse the current app instance.
Yes, you can reuse if the app is in background. In PendingIntent you will pass an Intent, so in that Intent you should set a flag like intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); so if the app is in background it will just bring activity to front.
According to android docs :
FLAG_ACTIVITY_NEW_TASK
Added in API level 1 int FLAG_ACTIVITY_NEW_TASK If set, this activity
will become the start of a new task on this history stack. A task
(from the activity that started it to the next task activity) defines
an atomic group of activities that the user can move to. Tasks can be
moved to the foreground and background; all of the activities inside
of a particular task always remain in the same order. See Tasks and
Back Stack for more information about tasks.
This flag is generally used by activities that want to present a
"launcher" style behavior: they give the user a list of separate
things that can be done, which otherwise run completely independently
of the activity launching them.
When using this flag, if a task is already running for the activity
you are now starting, then a new activity will not be started;
instead, the current task will simply be brought to the front of the
screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK
for a flag to disable this behavior.
This flag can not be used when the caller is requesting a result from
the activity being launched.
Check the docs here
But if the user didn't navigate to the SecondActivity i want to open the HomeActivity.
Why do you launch HomeActivity if your notification intent class you are creating have SecondActivity as the launcher. It will create a new SecondActivity and launches it if it is not in background.
EDIT: If you want to relaunch app where you left off try like this :
final Intent resumeIntent = new Intent(context, YourLauncher.class);
resumeIntent.setAction("android.intent.action.MAIN");
resumeIntent.addCategory("android.intent.category.LAUNCHER");
final PendingIntent resumePendingIntent = PendingIntent.getActivity(context, 0, resumeIntent, 0);
Here YourLauncher.class is the launcher class name.
I have an application where after a certain amount of time I want all activities in the app's stack to be removed and replaced by a login activity screen. I've got a Runnable timer that issues this set of statements:
Intent intent = new Intent( ctx, mainActivity.class );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
ctx.startActivity( intent );
This works fine when my app is in the foreground, however, when my app is in the background and I'm using another app (say, the Web Browser), the timer fires as it should and pops up the login activity screen to the foreground.
What I want is for the login activity to be activated and moved to the front of the stack, and have all other activities in the stack removed, but not have the app and activity popped to the foreground over any currently running app.
Is there any way to do this with a different method or flag? Thanks.
You could probably finish() the login activity in the onCreate event, conditional on an intent extra. Hopefully it would then be closed before it is displayed...
I know there's been a few posts for what I'm about to ask but I can't find any with the right answer.
From my understanding, if your main activity's (let's call it A) launchMode is set to singleTask, and A has initiated activity B then a click to the home button will destroy the history stack and re-launching the application will take you back to A and not B.
I have launchMode set to singleTask because I have a persistent notification and I don't want to have multiple instances of the main activity to appear whenever the user clicks on the notification.
Is there something I'm missing that would allow me to cater for both?
So I'm asking if there's a way I can ensure that whenever the user wishes to launch the app, from the notification or not, to take him back to the last (current) activity.
If I change launchMode to singleTop it works but I get multiple instances of the main activity whenever I launch it.
Thanks
Andreas
Have you tried setting launchMode to singleTop to all the activities in your app?? Because what i get from your query is that the main activity isn't singleTop, so that might lead to another instance of the main activity being called once the main activity is launched from the activity that was launched from the notification activity.
Or you can specify the launchMode as an attribute to the application tag itself in the manifest.
I use the following code to avoid multiple instances of the activity
Intent intent=new Intent(this,RICO.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
Changing manifest doesn't look appropriate to me
I'm having issues with both the approches.
The notification works flawless only in this condition:
- using the back button in the main activity (with the history containing only the that activity)
- not using the Home button
- not using the notification IF the activity you are calling is on top and active
In any other case, the notification cannot anymore call on the foreground the activity by using "new Intent(...)"
I've found the alchemical combination of manifest options and intent's flags for getting what I needed:
Intent intent= new Intent(this, YaampActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
using these options
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"
inside the element.
Now I've a notification which spawns the main activity (if that activity is not already in the foreground) and its behavior is correct even if the activity is "closed" by pressing the home button and/or the back one.