Button click lost on widget when screen is rotated(屏幕旋转时,小部件上的按钮单击丢失)
问题描述
我有一个非常简单的小部件应用程序,它由一个带有背景的 LinearLayout
和一个 ImageButton
组成.
I have a very simple widget application which consists of a LinearLayout
with a background and an ImageButton
.
在 AppWidgetProvider
onUpdate()
方法中,我注册了单击按钮以广播意图.当小部件第一次加载时,一切都运行良好并且点击被捕获.当屏幕旋转时出现问题,即使屏幕旋转回来也不会再次捕获点击.
In the AppWidgetProvider
onUpdate()
method, I register the click of the button to broadcast an intent. When the widget first loads, everything runs fine and the click is captured. The problem occurs when the screen is rotated, and the click is never captured again even if the screen is rotated back.
屏幕旋转时我需要做什么才能重新注册点击?
What do I have to do to re-register the click when the screen rotates?
下面是我正在使用的一些代码段.
below is some segments of code I am using.
AppWidgetProvider
AppWidgetProvider
@Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
if(intent.getAction().equals("test.CLICK"))
{
CallTestMethod(context);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews views=new RemoteViews(context.getPackageName(), R.layout.widget);
Intent clickintent=new Intent("test.CLICK");
PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.change_mode, pendingIntentClick);
SetInitialLayout(context);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
清单
<receiver android:name=".Widget" android:label="@string/widget_name">
<intent-filter>
<action android:name="android.appwidget.action.ACTION_APPWIDGET_CONFIGURE" />
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="test.CLICK" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_mode_switcher" />
</receiver>
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_layout"
android:layout_width="140dip"
android:layout_height="140dip"
android:scaleType="fitCenter"
android:orientation="vertical"
android:background="@drawable/background">
<ImageButton
android:id="@+id/change_mode"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:orientation="vertical"
android:src="QGRyYXdhYmxlL25vbmVfc2VsZWN0ZWQ="
android:background="@null"
android:clickable="true" />
</LinearLayout>
感谢大家的帮助!
推荐答案
这对我有帮助:Android小部件ImageButton在屏幕旋转时丢失图像
简而言之,您必须在每次调用 awm.updateAppWidget 之前注册点击次数 (views.setOnClickPendingIntent)
In short, you have to register the clicks (views.setOnClickPendingIntent) before EVERY call to awm.updateAppWidget
这篇关于屏幕旋转时,小部件上的按钮单击丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:屏幕旋转时,小部件上的按钮单击丢失


- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- URL编码Swift iOS 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- UITextView 内容插图 2022-01-01