title: Android: how to prevent multiple instances of an activity to be launched from a widget?
body: <p>Steps to reproduce the issue:</p>
  
  <ul>
  <li>the user launches my app (name of the root activity: "mainActivity") => instance A of mainActivity</li>
  <li>he presses the home button (mainActivity running in background)</li>
  <li>he installs the widget relative to this app</li>
  <li>he clicks on the widget => a new instance of mainActivity (instance B) is displayed</li>
  <li>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))</li>
  </ul>
  
  <p>Do you know how to avoid this issue ? (I have seen some similar questions on stackoverflow but not stricly what I wanted)</p>
  
  <p>Thanks !!!!</p>
  
  <p>The code:</p>
  
  <pre><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…///
  
      }
  </code></pre>