Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Opening wrong Activity from the widget

I have been struggling with this problem for a while now. I have a widget which displays summary of a cooking recipe. The widget is clickable so whenever user clicks on it the new activity is opened with a full description of that recipe. The problem is that sometimes when the widget is clicked it opens a wrong activity. Let me give you an example:

  1. User clicks on widget and activity A is opened
  2. User starts activity B (e.g. recipe index) from the activity A.
  3. User starts activity C from activity B.
  4. User presses Home button.
  5. User clicks on the widget again and the activity C is displayed instead of A

I tried setting a compibation of those three flags which helps in solving only a half of the problem:

openIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

By using above flags I managed to display a correct activity (A) until I do the following:

  1. User opens activity A from the widget
  2. User moves to activity B and opens activity A by clicking on new recipe
  3. New recipe is displayed to the user
  4. User clicks Home button and clicks on the widget.
  5. Activity A is displayed (the right one) but with different recipe.
  6. From now on if I move to different activities B or C and click Home and then widget again the activity A is not displayed but the one which was visible before pressing Home button.

I hope I explained it in enough details. I would be really grateful for your help because this thing drives me crazy!

Updated:

This is my pendingIntent:

Intent openIntent = new Intent(context, ProfanityDefinition.class);

openIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
openIntent.setData(Uri.parse(openIntent.toUri(Intent.URI_INTENT_SCHEME)));

Bundle bundle = new Bundle();
bundle.putBoolean("widgetRequest", true);
openIntent.putExtras(bundle);
PendingIntent openPendingIntent = PendingIntent.getActivity(context, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_content, openPendingIntent);

Thanks

like image 412
Marqs Avatar asked Sep 28 '11 09:09

Marqs


1 Answers

Show the code for creating the PedningIntent. If you are passing extras to your activity, be aware that two intents that differ only by extras are treated as equivalent. You probably want to pass the PendingIntent.FLAG_CANCEL_CURRENT when you create the PendingIntent to make sure your intent is not reused.

like image 196
Nikolay Elenkov Avatar answered Nov 05 '22 22:11

Nikolay Elenkov