Android,如何将Task带到前台?

发布于 2024-11-28 02:42:53 字数 895 浏览 1 评论 0原文

我的问题:

如何在使用以下规则的同时在自己的任务中启动新的活动。

1) 如果该 Activity 已作为另一个任务的根存在(在同一应用程序内),则将该任务置于前台。我不想创建该活动的另一个实例。我只希望它作为根的任务进入前台并显示任务的顶部活动。

注意:它一次只能是一个任务的根,并且只能作为任务的根而存在,而不是其他地方。

2)如果该活动不存在,则在其自己的任务中创建该活动。

为什么我要努力实现这个目标? 我在“活动”的底部创建了四个底部,其行为应类似于选项卡。因此,如果我按第二个“选项卡”,我希望显示与该选项卡关联的活动。但是,如果它已经存在并且位于其自己的任务的底部,那么我希望该任务与当前位于任务顶部的任何活动一起显示。

到目前为止我尝试过什么? 我搜索了 stackOverflow 并没有找到类似的问题。我读了 http://developer.android.com/guide /topics/fundamentals/tasks-and-back-stack.htmlhttp://blog.akquinet .de/2010/04/15/android-activites-and-tasks-series-intent-flags/

我想我需要使用 FLAG_ACTIVITY_NEW_TASK和/或亲和力,但不确定。请寻找一只手。

谢谢, 布拉德利4

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 application) then bring the Task to the foreground. I don't want another instance of the Activity to be created. I just want the Task that it is the root of to come to the foreground and the top Activity of the Task to be displayed.

Note: it will only be the root of one Task at a time and it will only exist as the root of the Task and nowhere else.

2) If the Activity doesn't exist then create this Activity in its own Task.

Why I'm trying to achieve this?
I created four bottoms at the bottom of my Activities that should behave like tabs. So if I press the second "tab" I want the Activity that is associated with that tab to be displayed. But if it already exist and is on the bottom of its own Task then I would like that Task to be displayed with whatever Activity that is currently on the top of the Task.

What have I tried so far?
I've searched stackOverflow and couldn't find a similar question. I read http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
and http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/

I think I need to use either FLAG_ACTIVITY_NEW_TASK and/or affinities but not sure. Looking for a hand please.

Thanks,
Bradley4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空心空情空意 2024-12-05 02:42:53

我能够为 Android 版本 >= 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);
               }
        }
    }
}

您还需要将这些添加到您的清单中:

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

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" />
世界和平 2024-12-05 02:42:53

您应该使用 FLAG_ACTIVITY_CLEAR_TOPFLAG_ACTIVITY_SINGLE_TOP 在您对 startActivity() 的调用中。

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

始于初秋 2024-12-05 02:42:53

您可以设置Activity启动模式 (android:launchMode) 在 AndroidManifest 中,以便在运行时不会创建自身的新实例,但在未运行时会正常启动。然后,您可以使用 Intent 启动 Activity

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文