Intent flags - How to start a new instance of activity and close other participants

I am not very familiar with the flags of intent, so please carry me.

I am making an application that displays material from a database in ViewPager. I also have a widget that from time to time draws a random string and displays it. Now, when I click on it, they take me to activity, where the same content is displayed, and now I can take action on it.

Everything works, however, when I have already started work, and then click on the widget, it seems that it creates a new instance, because when I click the "Back" button, I do not exit the application, but I got to the previous instance or what something.

Is there a way to start a new instance of the action, and if there were some previous instances that close them using flags?

I played with CLEAR_TOP and that, but I honestly don't know what I'm doing.

Thanks!

// EDIT

Intent.FLAG_ACTIVITY_CLEAR_TOP does not seem to be doing anything. Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP seems to bring the previous vertex and ignores the new or something else. Perhaps let me rephrase, I start the action from the start icon, then click the "Home" button and click on the widget, and it should output a new instance, where ViewPager.setCurrentItem sets the correct page (same as in widgets). If before there was no main activity, everything is fine, but if it was started, complete it.

MyWidgetProvider extends AppWidgetProvider @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Log.d("MainActivity", "MyWidgetProvider # onUpdate"); for (int i = 0; i < appWidgetIds.length; i++) { int appWidgetId = appWidgetIds[i]; Intent intent = new Intent(context, MainActivity.class); intent.putExtra("from_widget", true); //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); views.setOnClickPendingIntent(R.id.widget_container, pendingIntent); appWidgetManager.updateAppWidget(appWidgetId, views); } } 
+7
source share
3 answers

There are several ways to do this. If you want to create a new instance and close the current instance (provided that it is at the top of the activity stack), you need to set Intent.FLAG_ACTIVITY_CLEAR_TOP .

Another alternative would be to reuse the same instance of the action if it is already at the top of the stack. In this case, you need to set both Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP . If you do this, Android will not create another instance of the activity, but will call onNewIntent() your activity to deliver the new intent.

+9
source

Another flag is closed

 Intent.FLAG_ACTIVITY_CLEAR_TOP 

And the flag of the new instance,

 Intent.FLAG_ACTIVITY_NEW_TASK 
+2
source

My requirement was to start working (AbcActivity) with a new instance and close any other instance of one activity (AbcActivity), if any, as well as close other actions from above (if any). Solution for me:

 startActivity(new Intent(MainActivity.this,AbcActivity.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)); 
0
source

All Articles