Steps to reproduce the issue:
Do you know how to avoid this issue ? (I have seen some similar questions on stackoverflow but not stricly what I wanted)
Thanks !!!!
The code:
public class MyWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Build the intent to call the service//
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent openAppIntent = new Intent(context.getApplicationContext(), MainActivity.class);
openAppIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent openAppPendingIntent = PendingIntent.getActivity(context, 0, openAppIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.widgetLinearLayout, openAppPendingIntent);
//// ETC…///
}
Try using:
openAppIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
description here.
You can also use:
openAppIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
description here.
I propose using:
openAppIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
This will reuse already existing Activity
and onNewIntent
will be called. You can update the UI from there as required.
For more reference check:Developer.android.com - FLAG ACTIVITY SINGLE TOP
Edit
Launcher Activity is the one which has the following intent filter in AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
These flags will not work if the widget starts a new TASK which has it's own stack of activities.
I think the best would be to read this articleDeveloper.android.com - ACTIVITY TASK DESIGN
Let us know if you find anything.
I’ve successfully prevented multiple instances of an activity by adding this attribute to the manifest:
android:launchMode="singleInstance"
This essentially tells Android that this activity is the Highlander (“There Can Only Be One”), and will prevent it from creating multiple instances. Whenever the activity in question is opened, Android will either bring the existing instance to the front, or in some cases destroy and recreate the current instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With