Android - When launch the same activity from widget with different extras, how to prevent the same instance show up after returned from HOME button?

前端 未结 5 1824
我在风中等你
我在风中等你 2021-01-13 12:52

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

相关标签:
5条回答
  • 2021-01-13 13:31
    • 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).
    0 讨论(0)
  • 2021-01-13 13:31

    If you are using a different Action in your Intent and use the SingleTop or similar flag and override onNewIntent to detect the correct action that should do the trick. You need to be prepared to handle the intent in onCreate and onNewIntent.

    0 讨论(0)
  • 2021-01-13 13:39
    clickIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    

    Since: API Level 1 If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.

    This solved the same issue of mine while implementing widget activities.

    0 讨论(0)
  • 2021-01-13 13:48

    When you press home, most probably the activity will not be destroyed. It is put to pause state in such case. So, I guess your code to initiate the stock view would stationed in onCreate rather than onResume. So, moving those to onResume should solve the problem.

    0 讨论(0)
  • 2021-01-13 13:55

    You can try this:

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

    Your activity will not be seen in the history stack

    0 讨论(0)
提交回复
热议问题