设备方向更改时小部件丢失数据
Posted
技术标签:
【中文标题】设备方向更改时小部件丢失数据【英文标题】:widget lost data when device orientation change 【发布时间】:2013-02-05 09:31:00 【问题描述】:我开发了一个应用程序,它有小部件,它们运行良好,但是我改变了设备的方向,它显示得很好。但是有时当我更改方向数据时不会显示。任何人都可以帮助我,为什么这个问题会在某个时间发生。 这是代码
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
super.onUpdate(context, appWidgetManager, appWidgetIds);
Logger.d(TAG, "WidgetProvider ENTER onUpdate");
String str = null;
SharedPreferences prefs = context.getSharedPreferences("userInformation", Context.MODE_PRIVATE);
if (prefs != null)
str = prefs.getString("ImageURL", "").toString();
if (str != null && str != "")
Logger.d(TAG, "WidgetProvider (Account logined)");
for (int j = 0; j < appWidgetIds.length; j++)
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[j]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.facedroid_widget_layout);
remoteViews.setRemoteAdapter(appWidgetIds[j], R.id.listTasks, intent);
remoteViews.setViewVisibility(R.id.rl_complete_widget, View.GONE);
// The empty view is displayed when the collection has no items.
// It
// should be a sibling
// of the collection view.
/******** Click Listener for All external items ***********/
// Register an onClickListener
Intent clickIntent = new Intent(context, WidgetProvider.class);
clickIntent.setAction(CLICK_ACTION_FACEDROID_LOGO_BUTTON);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
PendingIntent pendingIntent_facedroid_logo = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.rl_topManagerBar_logo, pendingIntent_facedroid_logo);
clickIntent.setAction(CLICK_ACTION_COMMENT_BUTTON);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
PendingIntent pendingIntent_commentBtn = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.commentBtn, pendingIntent_commentBtn);
clickIntent.setAction(CLICK_ACTION_CAMERA_BUTTON);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
PendingIntent pendingIntent_cameraBtn = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.cameraBtn, pendingIntent_cameraBtn);
clickIntent.setAction(CLICK_ACTION_CHECKIN_BUTTON);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
PendingIntent pendingIntent_checkinBtn = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.checkinBtn, pendingIntent_checkinBtn);
clickIntent.setAction(CLICK_ACTION_REFRESH_BUTTON);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
PendingIntent pendingIntent_refreshBtn = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.refreshBtn, pendingIntent_refreshBtn);
/******** Click Listener for All external items ***********/
clickIntent.setAction(CLICK_ACTION_ITEM_CLICK);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[j]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.listTasks, toastPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[j], remoteViews);
else
Logger.d(TAG, "WidgetProvider (Account not logined)");
for (int j = 0; j < appWidgetIds.length; j++)
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.facedroid_widget_layout);
/******** Click Listener for All external items ***********/
// Register an onClickListener
Intent clickIntent = new Intent(context, WidgetProvider.class);
clickIntent.setAction(CLICK_ACTION_COMPLETE_WIDGET_CLICK);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[j]);
PendingIntent pendingIntent_widget_top_bar = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.rl_complete_widget, pendingIntent_widget_top_bar);
appWidgetManager.updateAppWidget(appWidgetIds[j], remoteViews);
Logger.d(TAG, "WidgetProvider EXIT onUpdate");
// super.onUpdate(context, appWidgetManager, appWidgetIds);
//这里是Onreceive方法的代码
@Override
public void onReceive(final Context context, Intent main_intent)
super.onReceive(context, main_intent);
Logger.d(TAG, "WidgetProvider ENTER onReceive");
// all the intents get handled by this method
// mainly used to handle self created intents, which are not
// handled by any other method
// the super call delegates the action to the other methods
// for example the APPWIDGET_UPDATE intent arrives here first
// and the super call executes the onUpdate in this case
// so it is even possible to handle the functionality of the
// other methods here
// or if you don't call super you can overwrite the standard
// flow of intent handling
String str = null;
SharedPreferences prefs = context.getSharedPreferences("userInformation", Context.MODE_PRIVATE);
if (prefs != null)
str = prefs.getString("ImageURL", "").toString();
if (str != null && str != "")
Logger.d(TAG, "WidgetProvider (Account logined)");
if (main_intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE))
Logger.d(TAG, "WidgetProvider UpdateAction");
String updateType = main_intent.getStringExtra("updateType");
Logger.d(TAG, "updateType=" + updateType);
if (updateType != null)
if (updateType.equals("single"))
final int appWidgetId = main_intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
updateAppWidgetSingle(context, appWidgetManager, appWidgetId);
else if (updateType.equals("multiple"))
final int[] appWidgetIds = (int[]) main_intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
updateAppWidgetMultiple(context, appWidgetManager, appWidgetIds);
else if (main_intent.getAction().equals(CLICK_ACTION_ITEM_CLICK))
Logger.d(TAG, "WidgetProvider ItemClick");
// int appWidgetId =
// main_intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
// AppWidgetManager.INVALID_APPWIDGET_ID);
int viewIndex = main_intent.getIntExtra(EXTRA_ITEM, 0);
LinkInfo info_s = null;
try
info_s = public_feed.DATA.get(viewIndex);
catch (Exception e)
e.printStackTrace();
if (e != null)
Log.e(TAG, "onReceive() " + e.getMessage().toString());
else
Log.e(TAG, "onReceive() e=null onReceive");
if (info_s != null)
Intent i = new Intent(context.getApplicationContext(), SinglePostActivityWidget.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("LinkInfo", info_s);
context.getApplicationContext().startActivity(i);
else if (main_intent.getAction().equals(CLICK_ACTION_FACEDROID_LOGO_BUTTON))
Logger.d(TAG, "WidgetProvider FacedroidLogoButtonClicked");
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName component_name = new ComponentName("com.platinumapps.facedroid", "com.platinumapps.activities.Splash_Activity");
intent.setComponent(component_name);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (intent != null)
context.startActivity(intent);
else if (main_intent.getAction().equals(CLICK_ACTION_COMMENT_BUTTON))
Logger.d(TAG, "WidgetProvider CommentButtonClicked");
Intent i = new Intent(context.getApplicationContext(), StatusShareICS.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(i);
else if (main_intent.getAction().equals(CLICK_ACTION_CAMERA_BUTTON))
Logger.d(TAG, "WidgetProvider CameraButtonClicked");
Intent i = new Intent(context.getApplicationContext(), PhotoSharePopup_widget.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(i);
else if (main_intent.getAction().equals(CLICK_ACTION_CHECKIN_BUTTON))
Logger.d(TAG, "WidgetProvider CheckinButtonClicked");
Intent i = new Intent(context.getApplicationContext(), widget_place_checkin_dialog.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(i);
else if (main_intent.getAction().equals(CLICK_ACTION_REFRESH_BUTTON))
Logger.d(TAG, "WidgetProvider RefreshButtonClicked");
refresh_indicator = true;
final int appWidgetId = main_intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, AppWidgetManager.INVALID_APPWIDGET_ID);
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.facedroid_widget_layout);
updateViews.setViewVisibility(R.id.pb_refreshing, View.VISIBLE);
updateViews.setViewVisibility(R.id.refreshBtn, View.INVISIBLE);
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
new Timer().schedule(new TimerTask()
@Override
public void run()
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.facedroid_widget_layout);
updateViews.setViewVisibility(R.id.pb_refreshing, View.GONE);
updateViews.setViewVisibility(R.id.refreshBtn, View.VISIBLE);
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
, 1000 * 10);
Intent intent = new Intent(context, WidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra("updateType", "single");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
context.sendBroadcast(intent);
else if (main_intent.getAction().equals(REFRESH_END))
Logger.d(TAG, "WidgetProvider RefreshEnd");
if (refresh_indicator)
int appWidgetId = main_intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, AppWidgetManager.INVALID_APPWIDGET_ID);
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.facedroid_widget_layout);
updateViews.setViewVisibility(R.id.pb_refreshing, View.GONE);
updateViews.setViewVisibility(R.id.refreshBtn, View.VISIBLE);
mgr.updateAppWidget(appWidgetId, updateViews);
refresh_indicator = false;
// if (WidgetService.isOrientationChanged)
// Log.e(TAG, "WidgetService.isOrientationChanged=true");
// WidgetService.isOrientationChanged = false;
// final int appWidgetId =
// main_intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
// 0);
// final AppWidgetManager appWidgetManager =
// AppWidgetManager.getInstance(context);
// updateAppWidgetSingle(context, appWidgetManager,
// appWidgetId);
// else
// Log.e(TAG, "WidgetService.isOrientationChanged=false");
//
else
Logger.d(TAG, "WidgetProvider (Account not logined)");
Toast.makeText(context, "Please login to facedroid", Toast.LENGTH_SHORT).show();
if (main_intent.getAction().equals(CLICK_ACTION_COMPLETE_WIDGET_CLICK))
Logger.d(TAG, "WidgetProvider HeaderWidgetClicked");
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName component_name = new ComponentName("com.platinumapps.facedroid", "com.platinumapps.activities.Splash_Activity");
intent.setComponent(component_name);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (intent != null)
context.startActivity(intent);
else if (main_intent.getAction().equals(CLICK_ACTION_WIDGET_CONTAINER_AREA))
Logger.d(TAG, "WidgetProvider ContainerWidgetClicked");
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName component_name = new ComponentName("com.platinumapps.facedroid", "com.platinumapps.activities.Splash_Activity");
intent.setComponent(component_name);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (intent != null)
context.startActivity(intent);
String updateType = main_intent.getStringExtra("updateType");
Logger.d(TAG, "updateType=" + updateType);
if (updateType != null)
if (updateType.equals("single"))
final int appWidgetId = main_intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
updateAppWidgetSingle(context, appWidgetManager, appWidgetId);
else if (updateType.equals("multiple"))
final int[] appWidgetIds = (int[]) main_intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
updateAppWidgetMultiple(context, appWidgetManager, appWidgetIds);
Logger.d(TAG, "WidgetProvider EXIT onReceive");
//super.onReceive(context, main_intent);
【问题讨论】:
【参考方案1】:当您旋转手机时,活动被破坏并重新创建。所以变量被重新初始化。这就是为什么data does not display
。你可以通过阻止活动 onRotation 的破坏来克服这个问题。要实现这一点,请将其放入您的清单中:
<activity android:name="FriendPickerActivity"
android:label="@string/app_name"
android:configChanges="orientation"/>
我指的是android:configChanges="orientation"
或者你可以在你的活动中使用onSaveInstanceState ()
和onRestoreInstanceState ()
【讨论】:
我开发的一个小部件有一个确切的问题。我将在代码中添加这个小技巧。让我们希望它能奏效!【参考方案2】:当设备的方向改变时,会调用 Activity 的 onCreate 方法。因此,您必须保留数据,然后在 onCreate 方法中检索它。看着 http://developer.android.com/guide/topics/resources/runtime-changes.html
【讨论】:
你的回答很好,和我的话题有关,但我已经处理了。这个问题只发生一段时间,而不是每次。此问题与 Super 类调用有关。以上是关于设备方向更改时小部件丢失数据的主要内容,如果未能解决你的问题,请参考以下文章
使用 QGraphicsView 时小部件背景不透明,但使用 QGridLayout 时透明
在底部表 GetX 库中调用 update() 时小部件不更新