Android的 - 当从部件具有不同的演员推出了同样的活动,如何prevent相同的实例显示从HOME键后返回?部件、实例、演员、推出了

由网友(分手后难过的大全)分享简介:我有一个包含4个按钮的窗口小部件,以显示4股票价格,他们每个人都会推出到同一个活动Quote.class显示股票信息。在的OnUpdate(),它会设置了额外的股票代码的pendingIntent。之后我打了一个按钮,它会引用的活动,显示了股票A,然后我打的返回按钮,主屏幕,报价活性调用的onDestroy(),当我打...

我有一个包含4个按钮的窗口小部件,以显示4股票价格,他们每个人都会推出到同一个活动Quote.class显示股票信息。在的OnUpdate(),它会设置了额外的股票代码的pendingIntent。之后我打了一个按钮,它会引用的活动,显示了股票A,然后我打的返回按钮,主屏幕,报价活性调用的onDestroy(),当我打按钮B,B股票将显示正常。然而,当我打HOME按钮后,它显示了股票A,报价单的活动只要求的onStop不调用的onDestroy(),然后为我打按钮B,它会调用ONSTART(),它显示了相同的情况下,显示股票A.

I have a widget that contains 4 buttons to show 4 stock prices, each of them will launch into the same activity Quote.class to show stock details. In onUpdate(), it will set up the pendingIntent with extras with stock symbol. After I hit button A, it goes to Quote activity that shows stock A. Then I hit the BACK button to the homescreen, Quote activity calls onDestroy() and when I hit button B, stock B will show properly. However, when i hit HOME button after it shows stock A, the Quote activity only calls onStop without calling onDestroy(), then as i hit button B, it will call onStart() and it 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);

}

原本我以为把一个标志的意图应该解决这个问题。但我已经试过 i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK或FLAG_ACTIVITY_NEW_TASK或FLAG_ACTIVITY_NO_HISTORY),他们没有什么差别。

Originally I thought adding a flag in the Intent should solve this problem. But I have tried i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK or FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_NO_HISTORY), none of them makes any difference.

那么,有没有办法让它工作?如何删除从HOME键历史堆栈?我怎么能叫的onCreate的报价活动,并获得新的临时演员时,我打B按钮?任何帮助是AP preciated。谢谢

So is there any ways to make it work? How can i remove the history stack from HOME button? How can I call onCreate in Quote activity and get new extras when i hit button B? Any help is appreciated. Thanks

推荐答案

这是正常的的onDestroy 来,如果你做简单的任务切换(如持有HOME键)可能不会被调用。如果你需要做清理工作,它需要去的onPause 。 我认为,问题是,你有一个 PendingIntent 只相差额外的费用。 PendingIntents被缓存,所以如果你使用两个具有相同的操作和数据,他们会互相覆盖。您可以规避通过给每个一些随机数据。尝试传递符号中的数据,而不是通过额外(这是无论如何pferred $ P $)。

It is normal for onDestroy to possibly not get called if you do simple task switching (like holding the HOME button). If you need to do clean-up, it needs to go in onPause. I believe the problem is that you have a PendingIntent that only differs by extra. PendingIntents are cached, so if you use two with the same action and data, they'll overwrite each other. You can circumvent that by giving each some random data. Try passing the symbol in the data rather than through the extra (which is preferred anyway).

阅读全文

相关推荐

最新文章