Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to prevent multiple instances of an activity to be launched from a widget?

Steps to reproduce the issue:

  • the user launches my app (name of the root activity: "mainActivity") => instance A of mainActivity
  • he presses the home button (mainActivity running in background)
  • he installs the widget relative to this app
  • he clicks on the widget => a new instance of mainActivity (instance B) is displayed
  • he clicks on the the back button: the user comes back to activity A (what I don't want ! The activity B should be closed (actually, the whole app should be closed))

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…///

    }
like image 460
Regis_AG Avatar asked Dec 07 '11 14:12

Regis_AG


3 Answers

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.

like image 152
skynet Avatar answered Nov 07 '22 08:11

skynet


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.

like image 32
Gyuri Majercsik Avatar answered Nov 07 '22 08:11

Gyuri Majercsik


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.

like image 1
user149408 Avatar answered Nov 07 '22 08:11

user149408