I'm developing a widget for my app and I'd like to pass specific data from the widget to the app. The user can add multiple versions of the widget and I need to detect which widget it is coming from so I can show specific information in the app.
What I have works so far, but only if I exit my app completely (repeatedly pressing the back button), otherwise it doesn't seem to be detecting the data being passed in.
This the code I have to pass the data from the widget to the app:
public class WidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i<N; i++)
UpdateWidget(context, appWidgetManager, appWidgetIds[i]);
}
public static void UpdateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
{
Intent intent = new Intent(context, Main.class);
final Bundle bun = new Bundle();
bun.putInt("widgetId", appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtras(bun);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.widgetText, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
UPDATE
Getting close to getting this working, thanks to Tal Kanel. I have an ActionBar
in my app and following Google's guidelines, I have this code in onOptionsItemSelected
:
if (item.getItemId() == android.R.id.home) {
final Intent intent = new Intent(activity, Main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return true;
}
If I click on the "back" button in the ActionBar
, then minimize my app and click on the widget this code is always null:
if (getIntent().getExtras() != null)
{
}
UPDATE 2
Actually, just going back to the main activity in my app causes getExtras()
to be null. I have this code in my app widget, that does not pass any extras to the app:
PendingIntent piMain = PendingIntent.getActivity(context, appWidgetId, new Intent(context, Main.class), PendingIntent.FLAG_UPDATE_CURRENT);
rv.setOnClickPendingIntent(R.id.widgetTitle, piMain);
If I click on that part in my widget, then go back and click on the part of my widget that is supposed to pass the extras, getExtras()
is always null.