Start / Resume Task Back Stack from Notification as Recent Apps Switching - android

My application has two activities A and B. A is the root activity and set with singleTop launchMode. B is started from A, i.e. the task stack is (A->B)
I add a status notification to launch the application as long press Home button. The notification intent point to activity A.
When the task stack is (A) only, the intent invokes onNewIntent() from existing A, as expected.
When the task stack is (A->B), the intent create new activity A. But what I wants is resume task (A->B) as switch recent apps by long press Home button.
Is the problem caused by incorrect launch mode used? or any flags need to be added to the notification intent?
Thanks.

You can use the following from your notification manager:
Intent intent = new Intent(context, ActivityA.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(intent);
Make sure that your ActivityA is not launched with FLAG_ACTIVITY_NEW_TASK.

Related

Destroy All Previous Activities After Specific Activity is Open [duplicate]

This question already has answers here:
Finish all previous activities
(28 answers)
Closed 7 years ago.
This is the scenario
Activity A -> Activity B -> Activity C -> Activity D (I would like to destroy Activity A, B, and C after Activity D is launched.
Any ideas please?
Intent intent = new Intent(ActivityC.this, ActivityD.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
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.
FLAG_ACTIVITY_CLEAR_TASK
If set in an Intent passed to Context.startActivity(), this flag will
cause any existing task that would be associated with the activity to
be cleared before the activity is started. That is, the activity
becomes the new root of an otherwise empty task, and any old
activities are finished. This can only be used in conjunction with
FLAG_ACTIVITY_NEW_TASK.
You need to pass flag Intent.FLAG_ACTIVITY_CLEAR_TOP with Intent :
Intent intent = new Intent(getApplicationContext(), D.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Use these lines to clear the activity stack when the Activity C is launched:
Intent i = new Intent(PresentActivityName.this, D.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();

How to resume an existing task in its current state

My application has two activities A and B. A is the root of the task, and is the one that is launched from the launch icon. B can be started from A.
As well as starting A from the launch icon, it is possible to launch A by clicking on a file in another application, e.g. clicking on an email attachment or a file in Drive. I have done this by adding actions and categories to the intent filter in the manifest file.
I want to make it so that when A is launched from another application, instead of creating a new task I want the existing task to resume in the same state it was in before. This could be activity A or B, wherever the user happened to be before they pressed home.
I have tried all kinds of launch modes and intent flags but nothing seems to work.
Broadcast an intent that's identical to the launcher intent in your manifest:
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
I don't know why, but this raises the existing task instead of starting a new one. By contrast, a launcher intent obtained the "official" way will actually start a new task:
Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(context.getPackageName());
change launchmode to singletask. and listening onNewIntent()

Restoring an activity stack from a notification?

I have a notification that create from an activity in a stack, and it's important that the stack be restored. Each activity in the stack should only be there a single time.
So I have this:
A -> B, B starts notification
For my intent, I need to restore A -> B on the stack. I tried starting A, but the intent didn't seem to have any extras to start startActivity immediately.
What combination of flags and XML attributes do I need to restore a stack?
If the user is on B and taps the notification, it should not start a new stack. Reloading everything is fine, but it should not go A -> B -> B.
In your notification intent use
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Since you can't/shouldn't change the back button function of backing back across different Apps. In B activity use an 'Up' button to navigate back up the stack using something like the following
Intent upIntent = new Intent(this,ParentActivity.class);
upIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(upIntent);
finish();
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT will bring an existing activity to the front, else it will create it keeping the stack free from duplicated activities

Start Activity and Bring it to Front

I have a service that once it completes a task it launches an intent to start an activity from my application. Like this:
Intent i = new Intent(MyService.this, MyActivityToBringToFront.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
This service can be running even after my application has been closed. When this is the case and my application is closed I need it to bring the activity to the front of whatever the user is currently doing. So if the user is in a different app I need my activity to pop up in front. Is this possible. None of the flags had any effect. Basically I need it to be like the system phone application. When you get a phone call it always brings the phone to the front. How can I do this? Thanks!
First of all you need to keep context in memory to make intent's.
Secondary you need a mechanism that can re-obtaining your context every 24 hours because usually context stay alive in 24 hours.
After.
Launching Activity from service :
Intent dialogIntent = new Intent(getBaseContext(), myActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
source : https://stackoverflow.com/a/3607934/2956344
To push activity on top add
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction("android.intent.action.VIEW");
i.setComponent(ComponentName.unflattenFromString("com.example.package/com.example.package.activityName"));
startActivity(i);
Intent i = new Intent(MyService.this, MyActivityToBringToFront.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity( i);
And API reference is
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
For example, consider a task consisting of the activities: A, B, C, D.
If D calls startActivity() with an Intent that resolves to the
component of activity B, then C and D will be finished and B receive
the given Intent, resulting in the stack now being: A, B.
The currently running instance of activity B in the above example will
either receive the new intent you are starting here in its
onNewIntent() method, or be itself finished and restarted with the new
intent. If it has declared its launch mode to be "multiple" (the
default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same
intent, then it will be finished and re-created; for all other launch
modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be
delivered to the current instance's onNewIntent().
This launch mode can also be used to good effect in conjunction with
FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task,
it will bring any currently running instance of that task to the
foreground, and then clear it to its root state. This is especially
useful, for example, when launching an activity from the notification
manager.
launching an activity from a service isn't always a good idea. You may want to use notification for that. If the user clicks on the notification, thats the time you show the Activity.

Start fresh application every time

Right now, what is happening is when I press home-key, last active activity is displayed. But what I want is to start the application as new every time the user presses home key and start the app from launcher. Please help
I think you can override onPause() event when you pressing home button and in there you can implement finishing your activity.
with setting intent flag as FLAG_ACTIVITY_CLEAR_TOP on starting your Activity your back stack would be clear so with finishing the last activity there is no activity on the task and stack
While creating intent you can set the flag. for example
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

Categories

Resources