Alternative to Intent.FLAG_ACTIVITY_CLEAR_TASK
Asked Answered
F

6

34

I have two apps App-B launches App-A. If the user starts App B from inside App A I call finish on App-A so I have no problem.

If the user goes straight to App B from the Application drawer or long pressing home button then I implement the below which clears the task in App A first before applying all the extras. This has the desired affect but only works on API 11. On lower APIs the new task in APP-A will not change and extras putExtra will have no effect. Any alternative to FLAG_ACTIVITY_CLEAR_TASK? for API <=10?

        Intent i = new Intent("com.App-A");
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

Thanks

Jason

Flywheel answered 19/6, 2012 at 9:37 Comment(7)
Does not clear the stack if it exists already.....Flywheel
Do you really mean 2 different applications? or are you talking about 2 Activities within the same application? Post the relevant parts of your manifest and we can help you more.Local
Yes two different applications the one being launched is SingleTask it has to be but not for this part of the functionality.Flywheel
Sorry, I still don't understand what you are trying to do. You say 'the one being launched is singleTask'. Is that app-A or app-B? Are you trying to create 2 separate tasks out of this? You've not provided enough information. Please explain in more details what you are trying to accomplish. Perhaps there is a better/different way to get the same results.Local
app-A is the one being launched. No only one task at anyone time.Flywheel
Have you had a look at my answer? Have you resolved the problem yourself?Local
@JasonSmith It's now possible to use IntentCompat to support backward, see my answer for link and sample.Wimberly
W
15

The new IntentCompat should've helped on that, but apparently the flag is ignored for API lower than 11.

To use IntentCompat do following:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
Wimberly answered 6/6, 2014 at 18:53 Comment(2)
Wish it were true, but: "This flag will only be obeyed on devices supporting API 11 or higher." From the docsUmbelliferous
@Umbelliferous I've completly overlooked that. But then WHATS THE POINT!. I guess we're free of doing if(api>x) but still, not very helpfull.Wimberly
A
2

this will work correctly

i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY | 
               Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Aspiration answered 11/12, 2012 at 9:4 Comment(0)
T
1

The best documentation I've found for these Intent flags is here: http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/

I don't understand what you are trying to do, but have you tried FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET?

Thiourea answered 29/9, 2012 at 0:23 Comment(0)
G
1

I may be wrong in understanding what you are asking, but is it that when you launch B, you want A to be killed?

In A, add this to the activity tag in the manifest:

android:noHistory=true

This will cause the activity to be removed from memory as soon as it loses focus.

Georgettageorgette answered 1/11, 2012 at 9:11 Comment(0)
L
0

I'm still having a lot of trouble understanding the problem but would like to help you fix it. Since comments only allow 600 characters and don't format well, I'm going to create an answer instead, because I'm sure that together we can get this resolved.

I would like to be able to reproduce your problem. To do that I've created 2 applications: AppA and AppB. AppA has a single activity called ActivityA and AppB has a single activity called ActivityB. Both ActivityA and ActivityB use android:launchMode="singleTask".

ActivityA has a button on it that launches AppB and finishes, like this:

    Intent intent = new Intent("de.sharpmind.example.AppB");
    intent.putExtra("extra", "launched from AppA");
    startActivity(intent);
    finish();

ActivityB has a button on it that launches AppA like this:

    Intent intent = new Intent("de.sharpmind.example.AppA");
    intent.putExtra("extra", "launched from AppB");
    startActivity(intent);

This all works as I expect it to. AppA and AppB run in different tasks. The "extra" is properly seen in the onCreate() methods of each application.

So, can you please tell me more about your problem. How can I reproduce exactly your problem? You wrote:

On lower APIs the new task in APP-A will not change and extras putExtra will have no effect.

What do you mean by that? Where are you putting extras and where are you getting them and what do you expect to happen?

Also, what is the launchMode of your AppB?

Also, when you have this problem, are there other activities in the task stack for AppA?

Please provide more info, either in your original question or here as comments.

Local answered 26/9, 2012 at 14:43 Comment(0)
P
0

Using FLAG_ACTIVITY_CLEAR_TASK clears the back stack. If I'm understanding correctly, this is the behavior you want.

Using singleInstance instead of singleTask in your manifest will do this.

In the comments you said that it must be singleTask. I'm assuming this is because you need the back stack in certain circumstances.

Since launchMode can't be changed programaticaly and FLAG_ACTIVITY_CLEAR_TASK is not availble for API <=10, you may have to create two identical activities.

One with launchMode=singleTask and one with launchMode=singleInstance.

Add this to the one using singleInstance to get a clear stack when launched from the app drawer:

 <category android:name="android.intent.category.LAUNCHER" />
Pantelleria answered 22/10, 2012 at 1:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.