0

I am currently making an app that containing a widget. When placing the app a WidgetConfigure Activity opens where you can select an object out of a recycleViewer.

The object (Kitten) implements the parcelable and the recyleviewer is filled by an adapter class.

When the eventListner of that recyleviewer is triggered, the selected object must be passed to the widget since I need properties from that object in my widget.

I'm currently completely stuck and confused how I can pass an object to the widget. How can I pass an object to the widget?

Here is my code that will run in the eventListner in 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);
}

Also I need to find a way when the user tapping this widget, a certain activity of my app will open that will receive that object.

Summery of the problem (I don't have the privilege to post an image yet)

neqopa
  • 119
  • 1
  • 12
  • You can implement the class with Serializable . In that way your object can pass through Intent. – MezzDroid Nov 17 '17 at 22:15
  • The problem is that the intent for a widget is a bit different than a regular activity. Normally I pass objects using parceable in a similar manner: https://stackoverflow.com/questions/42266436/passing-objects-between-fragments This is how an intent looks like for a widget: Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, ChecksWidgetProvider.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {mAppWidgetId}); sendBroadcast(intent); But I'm confused since it NEED id's, I don't know how to pass an object or a list to it. – neqopa Nov 18 '17 at 12:10

0 Answers0