Android When you start the same activity from a widget with various additional functions, how to prevent the same instance from appearing after returning from the HOME button?

I have a widget that contains 4 buttons to show 4 stock prices, each of which will launch in the same type of activity Quote.class to show details. In onUpdate (), it will configure pendingIntent with additional functions with a stock symbol. After I press the A button, it will go to the citation activity, which shows the stock A. Then I press the BACK button on the desktop, the onDestroy () activity request is called, and when I press the B button, the B indicator will be displayed correctly. However, when I press the HOME button after it shows the stock, the Quote activity only calls onStop without calling onDestroy (), and then when I press the B button, it is called onStart () and shows the same instance that shows stock A.

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.etappwidget); setQuoteIntent(context, views, R.id.view1, "BAC", 1); setQuoteIntent(context, views, R.id.view2, "C", 2); setQuoteIntent(context, views, R.id.view3, "GOOG", 3); setQuoteIntent(context, views, R.id.view4, "AAPL", 4); private static void setQuoteIntent(Context context, RemoteViews views, int viewId, String symbol, int requestCode) { Intent i = new Intent(context, Quote.class); i.putExtra("SYMBOL", symbol); i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); PendingIntent pi = PendingIntent.getActivity(context, requestCode, i, PendingIntent.FLAG_UPDATE_CURRENT); views.setOnClickPendingIntent(viewId, pi); 

}

Initially, I thought that adding a flag to Intent should solve this problem. But I tried i.setFlags (Intent.FLAG_ACTIVITY_MULTIPLE_TASK or FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_NO_HISTORY), none of them have any meaning.

So are there any ways to make it work? How to remove history stack from HOME button? How can I call onCreate in Quote Quote and get new additions when I press button B? Any help is appreciated. thanks

+4
source share
5 answers
  • As a rule, it is not possible for onDestroy to call a call if you perform a simple task switching (for example, holding the HOME button). If you need to perform a cleanup, you need to go into onPause .
  • I believe the problem is that you have a PendingIntent , which is different only by the extra. PendingIntents are cached, so if you use two with the same action and data, they overwrite each other. You can get around this by giving everyone random data. Try to pass a character in the data, and not through an additional one (which is preferred in any case).
+2
source

You can try the following:

  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 

Your activity will not be displayed on the history stack

+1
source
 clickIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 

Because: API Level 1 If set, new activity is not saved in the stack history. As soon as the user moves away from him, the activity is completed. It can also be set using the noHistory attribute.

This solved the same problem as when working with widgets.

+1
source

When you click on a house, most likely the activity will not be destroyed. In this case, it is paused. So, I assume that your code to run the stock view will be placed in onCreate , not onResume . So moving these objects to onResume should solve the problem.

0
source

If you use another action as you intend and use SingleTop or a similar flag and override onNewIntent to find the correct action that the trick should do. You should be prepared to handle the intent in onCreate and onNewIntent.

0
source

All Articles