In my application, I want to programmatically bring the most recently used third party to the front. Looking at the other answers here, I tried to restart the activity using baseIntent returned from the list of recent tasks, but this does not seem to lead to activity to the front, no matter what happens.
My ultimate goal is to create an application that replaces the incoming call screen with a small overlay so that the user is not completely removed from any application that they use when they receive the call. I found that you cannot replace the default incoming call screen (if this is not correct, let me know how I would prefer to do it instead) so that the workaround I am trying to call the last used application is in front of the screen (to overlay the incoming call screen ), and then display my overlay on top of this.
Here's the code I'm using (the action runs from the broadcast receiver)
public class BringMRUAppToFront extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityManager activityManager = (ActivityManager) getSystemService("activity"); List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED); if(recentTasks.size() > 2) { RecentTaskInfo recentTask = recentTasks.get(2); Intent testIntent = recentTask.baseIntent; Log.i("MyApp", "Recent task - " + recentTask.baseIntent.getComponent().getPackageName()); testIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(testIntent); } finish(); } }
Is there a reliable way to transfer any outside activities to the front? Activity is guaranteed in memory (if it is not in memory, then I would just display the initial screen), so there should be no problems. I also don’t think that this will be a security problem in this case, since it will just show the application that was visible right before the phone rang - although I understand that opening this as a whole in the SDK can be a risk ... hoping this is possible.
EDIT: Changed the code a bit to reflect what I'm doing. The desired task will almost always be the third task on the list — the first current task, and the second — the caller’s task. I can bring the task to the forefront, but it is not always in the same state (for example, on the browser page instead of the settings screen in the browser). How does the recent task list do it?
source share