4

I have two widgets A and B. I have two issues:

  1. On tapping a widget (A or B) multiple times, multiple instances of the same activity opens up.
  2. Sometimes Widget A opens widget Bs activity and vice versa.

Here is my code:

Widget A Provider class:

 for (int appWidgetId : appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.favorite_flavor_widget);
        Intent intent = new Intent(context, SplashActivity.class);
        intent.setAction("FromFavoriteFlavorWidget");
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        intent.putExtra("FromFavoriteFlavorWidget", true);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.favoriteFlavorBox, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }

Widget B Provider class:

 for (int appWidgetId : appWidgetIds) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.report_food_widget);
            Intent intent = new Intent(context, SplashActivity.class);
            intent.setAction("FromReportFoodWidget");
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("FromReportFoodWidget", true);
            PendingIntent pendingIntent = PendingIntent.getActivity(context,
                    0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.reportFoodBox, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }
android_eng
  • 1,370
  • 3
  • 17
  • 40

2 Answers2

2

settings flags as Intent.FLAG_ACTIVITY_CLEAR_TOP Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

and try adding this to manifest.

android:launchMode="singleTask"

and

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent );

http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

Sree Reddy Menon
  • 1,301
  • 13
  • 23
  • Actually it works when the app is completely closed but if the app is in background it does not. – android_eng Feb 20 '16 at 19:01
  • http://stackoverflow.com/questions/4341600/how-to-prevent-multiple-instances-of-an-activity-when-it-is-launched-with-differ – Sree Reddy Menon Feb 20 '16 at 19:05
  • Just tell me when you are pressing back from activity. navigation issue ? right then you supposed to use TaskStackBuilder along with PendingIntent please go through http://developer.android.com/intl/es/training/notify-user/navigation.html – Sree Reddy Menon Feb 20 '16 at 19:14
  • The issue now is that if the app is in the background and I tap on the widget, it does not open the correct fragment, it just opens the app and shows the existing fragment. – android_eng Feb 20 '16 at 19:18
  • why dont you pass a boolean variable from widget via intent and when the boolean variable is true on activity , then replace the current fragment with required fragment. – Sree Reddy Menon Feb 20 '16 at 19:23
  • I am doing that. if (getIntent() != null && getIntent().getExtras() != null) { if (getIntent().getBooleanExtra(Constants.PUSH_NOTIFICATION_EXTRA, false) || getIntent().getBooleanExtra("FromLiveFeedWidget", false)) { onNavigationItemSelected(navigationView.getMenu().getItem(5)); } else if (getIntent().getBooleanExtra("FromFavoriteFlavorWidget", false)) { onNavigationItemSelected(navigationView.getMenu().getItem(3)); } } – android_eng Feb 20 '16 at 19:27
  • 1
    I found the solution. Just had to put those checks in onNewIntent call. – android_eng Feb 20 '16 at 19:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104059/discussion-between-sree-reddy-menon-and-rohit-ramkumar). – Sree Reddy Menon Feb 20 '16 at 20:00
0

Why are u calling

appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

2 times call it only one time and use android:launchMode="singleTop"for multi instance issue

JAAD
  • 12,349
  • 7
  • 36
  • 57