Android, how to bring a Task to the foreground?

后端 未结 4 1974
情书的邮戳
情书的邮戳 2020-12-16 05:25

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 Tas

4条回答
  •  春和景丽
    2020-12-16 06:16

    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 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:

    
    
    

提交回复
热议问题