0

I have an activity with launch mode as Single Task.The activity is launched by with intent flags Intent.FLAG_ACTIVITY_NEW_TASK by a Broadcast receiver . If this activity is on top of home screen and if I call moveTasktoBack(true) from this activity, I expect the home screen to be shown however instead of home screen, Activity B from another task is brought in front. The sequence of operations is as follow.

Activity B in TASK B -> [Press Home Button] -> Home Screen -> Launch Activity A by BroadCast Receiver -> Activity A calls moveTaskToBack(true) -> Activity B in Task B comes to foreground.

I have checked the Task Affinity of Task A (with activity A) and Task B (with activity B) and they are different.

How can I make sure that in such a scenario, Home screen is shown when activity moves itself to back of stack.

apoorvn
  • 43
  • 5

2 Answers2

0

Instead of moving your task to the background, just launch the HOME screen like this:

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • I can't always do that as the use case I have will require the Activity B to be popped up in cases when Activity A is launched on Top of Activity B and calls Activity A calls movetasktoBack. Activity B in this case can be from any application and is in most cases will not be the part of same task as Activity A. – apoorvn Feb 07 '15 at 05:47
  • Your comment makes no sense. This code accomplishes the same thing as calling `moveTaskToBack()`. It moves the task that the calling `Activity` is in into the background and shows the user the HOME screen. This is exactly what you said you wanted to do. I don't understand your complaint. Please try this and then tell me what doesn't work the way you want. – David Wasser Feb 07 '15 at 11:41
  • I guess am hitting the following bug in AOSP. https://gitorious.org/cyandreamproject/android_frameworks_base/commit/04f0b70c13c0b89b11493ff3f26ab2d0d961bd3a – apoorvn Feb 08 '15 at 09:13
  • we have a custom device and we fork Android to put on our device. Our Android might not have this fix. As I said, I do not always want to go back to home screen, if I am on top of different Activity. In my scenario, although I am on Top of Home Screen, on calling the function moveTasktoback, home screen is not shown but if I am on top of any other activity things works as expected. – apoorvn Feb 08 '15 at 09:21
  • You wrote _...with launch mode as Single Task_ in your question. Since your `Activity` is launched as `singleTask` it can't be launched on top of another `Activity`. It will always be the root of a new task. In which case, my code should do what you want. However, if there is an Android bug that is causing bad behaviour... – David Wasser Feb 08 '15 at 09:43
  • @VSim didn't post a link. That was apoorvn – David Wasser Sep 19 '18 at 10:10
  • Yes. As I understand, by default my comment refers to the OP (the one who asked the question.) – VSim Sep 19 '18 at 11:37
  • @VSim I think, since your comment is on my answer, I'm the only one who gets notified. – David Wasser Sep 19 '18 at 15:03
  • @DavidWasser Sounds reasonable. I'll re-issue the comment. – VSim Sep 20 '18 at 10:27
  • @apoorvn Could you give some details about the bug you mentioned ? Your link seems to be broken. – VSim Sep 20 '18 at 10:28
  • @DavidWasser BTW, you know, he has a point that your proposed solution isn't equivalent to what he was trying to do. Launching the home screen is equivalent to moving the Activity to back only if there's no other Activity open. If there is, then launching the home screen from one of them will send ALL of them to the background, which maybe isn't what you want. Instead, killing the Activity (if that's acceptable in your situation) is more or less equivalent to sending it to back, no matter how many other open Activities there are. – VSim Sep 20 '18 at 12:07
0

I have experienced myself problems when trying to do moveTaskToBack (true) with more Activities (from different apps) in the stack and in an automatic situation (that is, programmatically on receiving a broadcast event). I'm not sure what the exact cause is. But I solved them using the nuclear option: instead of trying to move the Activity / task to back, I just killed it using finish (). It works well, in all situations I suppose. Might slow things down a little bit so use with caution, but in my case it solved the problem. And, of course, you'd need to save all relevant state info and restore it. In my case that wasn't a big problem.

VSim
  • 161
  • 3
  • 10