Android AppWidget 配置 - 在新任务中启动

发布于 2024-10-20 18:33:07 字数 1283 浏览 5 评论 0原文

我一直在寻找这个!

所以我有一个应用程序小部件,它有一个配置活动,我可以在按下应用程序上的按钮时打开该活动。场景是:

应用程序已打开。

使用主页按钮关闭应用程序。

选择添加我的小部件

我已经配置了小部件。

放置在我的主屏幕上

然后使用小部件上的按钮再次打开配置活动。

按“返回”取消新配置将使我返回应用程序。

当我按下回击键时,我只想回家。

基本上我要问的是。如何在其自己的任务/堆栈中启动配置活动?

我已经研究过意图过滤器,但我不太确定,或者可能与它所在的包有关,或者也许这是不可能的!

我想这可能与我用来启动配置活动的意图有关

 Intent configIntent = new Intent(this, Configuration.class);
 configIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);      
 remoteView.setOnClickPendingIntent(R.id.config, PendingIntent.getActivity(this, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT));

也许因为我用“this”作为上下文启动它,它总是会在我的应用程序堆栈中启动...... 但待处理的意图 api 是: PendingIntent API 1 “请注意,该活动将在现有活动的上下文之外启动”

所以是的,我现在不再说话了,因为我最终只是在兜圈子!

编辑

因此在清单中尝试了android:launchMode="singleInstance",如所述。这有效,但是它阻止了“startActivityForResult”行为的正常工作。 (这是配置活动的全部原因)收到错误:

警告/ActivityManager(59):活动正在作为新任务启动,因此正在取消活动结果。

所以还是没有找到解决办法。

I've searched and searched and searched for this!

So I've got an app widget and it has a configuration activity that I can open when pressing a button on the app. The scenario is:

Had the app opened.

Closed the app with the home button.

Selected to add my widget

I have configured the widget.

Placed on my home screen

Then open the configuration activity again with the button on the widget.

Cancel the new config by pressing back will put me back into the app.

When pressing back I want to just return home.

Basically what I'm asking is. How do I start the configuration activity in it's own task/stack?

I've looked into intent filters but I'm just not quite sure, or maybe it's something to do with the package it's in, or maybe it's just not possible!

I suppose it may have something to do with the intent I use to launch the config activity

 Intent configIntent = new Intent(this, Configuration.class);
 configIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);      
 remoteView.setOnClickPendingIntent(R.id.config, PendingIntent.getActivity(this, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT));

Perhaps because I launch it with 'this' as the context, it will always start in my applications stack...
but the pending intent api is:
PendingIntent API 1
"Note that the activity will be started outside of the context of an existing activity"

So yeah I'll stop talking now as I just end up going in circles!

EDIT

So tried android:launchMode="singleInstance" in the manifest like was stated. This worked however it stops the 'startActivityForResult' behaviour working correctly. (which is the whole reason for a config activity) Get the error:

WARN/ActivityManager(59): Activity is launching as a new task, so cancelling activity result.

So still haven't found a solution.

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

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

发布评论

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

评论(2

彼岸花似海 2024-10-27 18:33:07

好的,已排序:-) 需要:

android:taskAffinity=""

在清单中,将任务亲和力设置为空字符串允许活动在其自己的堆栈中启动,因为它与应用程序的其余部分不“附属”。

更新

我已将任务亲和力更改为:
android:taskAffinity="com.my.package.alternative.task"
每次我启动该活动时,它都会在“历史记录”中多次出现。因此,它现在在自己的堆栈中启动,但与同一活动的其他实例共享。

还需要将 Flag Intent.FLAG_ACTIVITY_NO_HISTORY 添加到您的意图中:-) 这会停止当您“按住”主页按钮时,您多次获取应用程序的历史记录。

更新

我注意到 FLAG_ACTIVITY_NO_HISTORY 没有执行我想要的操作,我已将其删除并添加:

android:excludeFromRecents="true"

到清单中的活动标记中。该活动现在的行为就像我想要的:-)

从以下链接路径中得到了这个答案:
任务和任务返回堆栈 |
管理任务 |
隶属关系标签

Ok sorted it :-) needed:

android:taskAffinity=""

in the manifest, setting the task affinity to an empty string allows for the activity to start in it's own stack, as it is not 'affiliated' with the rest of the application.

UPDATE

I have changed the task affinity to:
android:taskAffinity="com.my.package.alternative.task"
as each time I launched the activity it was showing up multiple times in the 'history'. So it now starts in it's own stack but is shared with other instances of the same activity.

Also need to add the Flag Intent.FLAG_ACTIVITY_NO_HISTORY to your intent :-) this stops your getting your application multiple time's in the history when you 'press and hold' the home button.

UPDATE

I've noticed FLAG_ACTIVITY_NO_HISTORY wasn't doing what I wanted, I've removed it and added:

android:excludeFromRecents="true"

into the activity tag in the manifest as well. The activity now behaves like I want :-)

Got this answer from the following link trail:
Tasks & Back Stack |
Managing Tasks |
Affiliation Tag

太傻旳人生 2024-10-27 18:33:07

尝试在 AndroidManifest.xml 中为应用程序的活动添加 android:launchMode="singleInstance"

Try to put android:launchMode="singleInstance" for an activity of the app in AndroidManifest.xml

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