我目前正在制作一个包含小部件的应用程序。放置应用程序时,会打开一个 WidgetConfigure Activity,您可以在其中从 recycleViewer 中选择一个对象。
对象(Kitten)实现了parcelable,recyleviewer 由适配器类填充。
当触发该 recyleviewer 的 eventListner 时,必须将所选对象传递给小部件,因为我需要小部件中该对象的属性。
我目前完全卡住并困惑如何将对象传递给小部件。 如何将对象传递给小部件?
这是我将在 eventListner 中运行的代码
LongEventConfigureActivity
:
private void makeWidget(Event myObj){
// How I normally passing data to an activity/fragment
//Intent i = new Intent();
//Bundle b = new Bundle();
//b.putParcelable(CONST_PARCELKEY, myObj);
//i.putExtras(b);
//i.setClass(this, ObjectDetailActivity.class);
//startActivity(i);
// Example of how to pass data to the widget
final Context context = LongEventWidgetConfigureActivity.this;
// When the button is clicked, store the string locally
String widgetText = txtWidgetText.getText().toString();
saveTitlePref(context, mAppWidgetId, widgetText);
// It is the responsibility of the configuration activity to update the app widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
LongEventWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
@Override
public void onItemClick(Event e) {
makeWidget(e);
}
我还需要找到一种方法,当用户点击此小部件时,我的应用程序的某个活动将打开,该活动将接收该对象。