Skip to content Skip to sidebar Skip to footer

Android, How To Bring A Task To The Foreground?

My question: How can I launch a new Activity in its own Task while using the following rules. 1) If the Activity already exists as a root of a another Task (within the same appl

Solution 1:

I was able to solve this for Android version >= Honeycomb:

@TargetApi(11)
protected void moveToFront() {
    if (Build.VERSION.SDK_INT >= 11) { // honeycomb
        final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

        for (int i = 0; i < recentTasks.size(); i++) 
        {
               Log.d("Executed app", "Application executed : " 
                       +recentTasks.get(i).baseActivity.toShortString()
                       + "\t\t ID: "+recentTasks.get(i).id+"");  
               // bring to front                
               if (recentTasks.get(i).baseActivity.toShortString().indexOf("yourproject") > -1) {                     
                  activityManager.moveTaskToFront(recentTasks.get(i).id, ActivityManager.MOVE_TASK_WITH_HOME);
               }
        }
    }
}

you also need to add these to your manifest:

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />

Solution 2:

You should use FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP in your call to startActivity().


Solution 3:

You can set the Activity's launch mode (android:launchMode) in the AndroidManifest so that it does not create new instances of itself if it is running, but will launch normally when it is not. Then you can start an Activity using Intent.


Solution 4:


Post a Comment for "Android, How To Bring A Task To The Foreground?"