简体   繁体   English

多个小部件:单击小部件时启动配置活动

[英]Multiple widgets: Launching configuration activity on widget click

following scenario: I have 3 of the same widgets on my home screen. 以下情况:我的主屏幕上有3个相同的小部件。 If one gets clicked, the widget configuration activity gets launched. 如果单击,则启动小部件配置活动。

This was implemented by following code: 这是通过以下代码实现的:

Intent intent = new Intent(context, WidgetConfigurator.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setOnClickPendingIntent(R.id.widget_linearlayout, pendIntent);

The launching is working, but there is one problem: 发射正在工作,但是存在一个问题:
1. Widget A gets clicked, configuration activity of Widget A is opened 1.单击窗口小部件A,打开窗口小部件A的配置活动
2. User hits "back" key, configuration activity disappears 2.用户按下“返回”键,配置活动消失
3. Widget B gets clicked, configuration activity of Widget B is opened 3.单击小部件B,打开小部件B的配置活动
4. User hits "back" key 4.用户点击“返回”键
=> Now the configuration activity of Widget A is shown =>现在,显示小部件A的配置活动

I always only want the "actual" configuration activity (fitting to the widget that was clicked) to be shown. 我始终只希望显示“实际”配置活动(适合单击的小部件)。 Which settings do i have to use for the Intent / PendingIntent? 我必须为Intent / PendingIntent使用哪些设置?

thx for any help 感谢任何帮助

Change your pending intent code to include the widget id as android reuses intents. 更改待处理的意图代码,以在Android重用意图时将小部件ID包括在内。 I wasted many hours on this with the wrong intent being sent on click. 我在此上浪费了很多时间,并在点击时发送了错误的意图。

PendingIntent pendIntent = PendingIntent.getActivity(context, WIDGETID, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Seems after trying around a lot i now found a working solution for all android versions: 经过大量尝试后,我现在发现了适用于所有android版本的有效解决方案:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setOnClickPendingIntent(R.id.widget_linearlayout, pendIntent);

A way to get sure your current activity gets closed is to call finish(). 确保当前活动已关闭的一种方法是调用finish()。 An android app is build like stack, the activity A starts, if the user hits back, this activity isn´t closing. 一个像堆一样构建的android应用程序,活动A开始,如果用户回击,则该活动没有关闭。 It´s just lying under the new started activity B. If user hits back in activity B, then activity A will be shown. 它只是位于新开始的活动B下。如果用户回击活动B,则将显示活动A。 I never tried this, but if You call finish in your onBackPressed method, the current activity will be closed. 我从未尝试过这样做,但是如果您在onBackPressed方法中调用finish,则当前活动将关闭。 To do this, override onBackPressed in your Activitys. 为此,请在“活动”中覆盖onBackPressed。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM