0

I have to app stacks like this:

A->B-C->D

X->Y

In Y I want to use getLaunchIntentForPackage("package") to get to D, however I get to A. What launch mode should I use etc? I would like to use singleTask for ABCD if possible. Also when in D and I press home and launch via the Icon for A again then depending on launch mode I don't get to D (that I would like), but to A. DCB are cleared.

I tried a lot of things but I just don't understand how it works. I don't seem to get a consequent behaviour.

JohnyTex
  • 3,323
  • 5
  • 29
  • 52
  • I wrote an answer to my problem (using other links). See: [Android: bug in launchMode=“singleTask”? -> activity stack not preserved][1] [1]: http://stackoverflow.com/questions/2417468/android-bug-in-launchmode-singletask-activity-stack-not-preserved/26359365#26359365 – JohnyTex Oct 14 '14 at 11:19
  • I wrote an answer to my problem here: **[Android: bug in launchMode=“singleTask”? -> activity stack not preserved][1]** [1]: http://stackoverflow.com/questions/2417468/android-bug-in-launchmode-singletask-activity-stack-not-preserved/26359365#26359365 – JohnyTex Oct 14 '14 at 11:20

1 Answers1

1

So what you observe is expected. The launch modes are defined per activity, not per app/package level.

  • So to get from Y to D, you need to start activity D using intent flag set to FLAG_ACTIVITY_NEW_TASK, this will ensure you get to the existing D, that is already on top in your first task (A->B->C->D)

  • To do the same from home screen (launcher icon) you may need to try below options for SingleTask mode, since you want to preserve the existing stack (A->B->C-D) , and simply want get to D; Quoting from the link:

    "+ For launchMode=singleTask if there is an intent_filter in the
    manifest the task stack is always cleared after returning to Home and
    re-launching (returns to main activity instead of last activity).
    
    + For launchMode=standard re-launch from Home instead returns to last
    activity in task (as expected).
    
    + If there is no intent_filter listed then even with
    launchMode=singleTask re-launch from Home returns to the last activity
    in the task"
    
Community
  • 1
  • 1
ashoke
  • 6,441
  • 2
  • 26
  • 25
  • I don't understand why singleTask would clear my stack? But more importantly: how can I restart another package (a stack with activities) from my other package (app) without clearing the stack? – JohnyTex Oct 14 '14 at 06:55
  • But I have to have an IntentFilter in singleTask otherwise it won't show up on Launcher -no? – JohnyTex Oct 14 '14 at 10:12
  • @JohnyTex change your main activity launch mode to standard, it will let you preserve the stack as-is (2nd `+`point above). You can still restart another package in `singleTask` mode using intent-flags (eg: [FLAG_ACTIVITY_NEW_TASK](http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK)) from your other app. Intent flags [override manifest launch modes, see here](http://developer.android.com/guide/components/tasks-and-back-stack.html#TaskLaunchModes). – ashoke Oct 14 '14 at 15:42
  • But I dont want to start a new task, I want to call back my singleTask stack(!) – JohnyTex Oct 15 '14 at 08:29
  • @JohnyTex it [wont start new task, just brings foreground existing task](http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK) (with D at the top). – ashoke Oct 15 '14 at 16:55
  • Thx, I will try it and if it works I owe you one, and then we can mark your comments as accepted answer. – JohnyTex Oct 16 '14 at 07:01
  • No it doesn't help. My singleTask app stack is cleared when pressing home and relaunching using GetLaunchIntentForPackage using FLAG_ACTIVITY_NEW_TASK! – JohnyTex Oct 16 '14 at 07:41
  • Declare it standard task, call D activity with that flag. – ashoke Oct 16 '14 at 08:00
  • No it doesn't work because then it launches the standard activity on top of the old stack. I just want to bring it back as it was. I want it to be singleTask, but it doesn't work! – JohnyTex Oct 16 '14 at 08:06
  • In your question u had 1st app abcd alredy active. If you had A as standard, then getting to d using that flag will work. Re-read my update to answer and last few comments – ashoke Oct 16 '14 at 08:10
  • OK so I how can I get launch intent for activity D using only package name? – JohnyTex Oct 16 '14 at 08:15
  • Doesnt matter as long as you had A as standard. Getting to D with that flag will just bring forward existing stack/task. You need to call D directly, cant use getp... Function as i mentioned in my answer – ashoke Oct 16 '14 at 08:15
  • OK thx! But what is the difference between launching from icon and getlaunchintentforpackage? How can I simulate launching from icon? – JohnyTex Oct 16 '14 at 08:17
  • You can directly call activity.. Look up explicit intent.sorry i am on a phone – ashoke Oct 16 '14 at 08:17
  • No worries. Answer if you like and have time! The problem I have is that I only have package name to work with... – JohnyTex Oct 16 '14 at 08:18
  • Both usually end up referring to main activity A... We need to call D directly – ashoke Oct 16 '14 at 08:18
  • But when people write launchers -how do they get from packages on disk to D? – JohnyTex Oct 16 '14 at 08:20
  • If I use getLaunchIntentForPackage(packageName) with Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED), then it works with SingleTop (and probably standard), but not SingleTask. It seems singleTask always resets when pressing home, which makes me wonder whats the practical difference to singleInstance. – JohnyTex Oct 16 '14 at 11:36
  • @JohnyTex SingleInstance keeps the task exclusive for that activity, no other activity allowed. So it ends up being just that one activity per task. – ashoke Oct 16 '14 at 15:25