Reputation: 43
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.
Upvotes: 0
Views: 2784
Reputation: 161
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.
Upvotes: 0
Reputation: 95578
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);
Upvotes: 0