Prevent new activity instance after clicking on no

2020-04-01 08:33发布

application (non-wanted) behavior -

  1. application is started, some text is put into text-box and notification is created through button action.
  2. user "clicks" the home button, application is "minimized", notification is available in bar
  3. user selects the notification and the application is "maximized"

BUT - instead of the original instance, new instance is started (e.g. in the newest instance is missing the original text; when the latest instance is closed there is still the original instance with original text ) .

the code of the notification method

Context context = getApplicationContext();
    CharSequence contentTitle = "someText1";
    CharSequence contentText = "someText2";
    Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
    notifyIntent.setClass(getApplicationContext(), RadioStream.class);
    PendingIntent intent = 
       PendingIntent.getActivity(RadioStream.this, 0, notifyIntent, 0);

    notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);

i have also in the manifest xml file following tag

android:launchMode="singleTask"

but it is still the same... The main problem is double/triple initialization of the application, i know that there are other means to preserve the values in resumed applications. Also it is needed that the applications stays running in background as the main functionality is the streaming of internet radio.

What is missing in the code ? What kind of information from my side is missing for to troubleshoot the issue ?

Thanks!

Dav

9条回答
Root(大扎)
2楼-- · 2020-04-01 09:02

You can set PendingIntent.FLAG_CANCEL_CURRENT on the PendingIntent:

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);

If the activity already exists,The new one will cancel and not update the old one! This works for me!

查看更多
趁早两清
3楼-- · 2020-04-01 09:03

Another option is to use setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP) on your Intent.

查看更多
在下西门庆
4楼-- · 2020-04-01 09:04

instead of :

notifyIntent.setClass(getApplicationContext(), RadioStream.class);
PendingIntent intent =   PendingIntent.getActivity(RadioStream.this, 0, notifyIntent, 0);

simply use these lines,it will helps you,

notifyIntent = new Intent();
PendingIntent intent= PendingIntent.getActivity(RadioStream.this, 0, notifyIntent, 0);
查看更多
Explosion°爆炸
5楼-- · 2020-04-01 09:07

reimplemented as service - everything works as expected

查看更多
戒情不戒烟
6楼-- · 2020-04-01 09:11

It is working for me by using below combination.

1) add launch mode in Manifest file android:launchMode="singleInstance"

2) set setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP) while creating intent.

intent = new Intent(this, StartUpActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);

查看更多
够拽才男人
7楼-- · 2020-04-01 09:23

add this flag to your activity properties.

android:launchMode="singleInstance"

查看更多
登录 后发表回答