Android, how to bring a Task to the foreground?

后端 未结 4 1969
情书的邮戳
情书的邮戳 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:07

    How about FLAG_ACTIVITY_REORDER_TO_FRONT?

    0 讨论(0)
  • 2020-12-16 06:11

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

    0 讨论(0)
  • 2020-12-16 06:11

    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.

    0 讨论(0)
  • 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<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" />
    
    0 讨论(0)
提交回复
热议问题