26

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

Neil Aitken
  • 7,856
  • 3
  • 41
  • 40
user375418
  • 271
  • 1
  • 3
  • 4

9 Answers9

24

What you refer as "application" is most probably an Activity. To avoid re-crating it when bringing up to front, use

android:launchMode="singleTop"

To have some code running background, you need to realize it as Service

ognian
  • 11,451
  • 4
  • 35
  • 33
15

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'll be posting code later today on `http://github.com/commonsguy/cw-andtutorials` (look at `20-Notifications`) that demonstrates this technique. – CommonsWare Jun 25 '10 at 10:02
  • 4
    This does the trick if you also have set android:launchMode="singleTop" for the given activity in the manifest. – Warpzit Dec 13 '12 at 12:57
  • I added Warpzit's comment to CommonsWare's solution, it's working for me. Thanks. – Lev Jul 08 '14 at 13:48
  • Think this @CommonsWare answer should be right answer. In addtiona I found that only using the `Intent.FLAG_ACTIVITY_SINGLE_TOP` flag worked for me. Including the `Intent.FLAG_ACTIVITY_SINGLE_TOP` too caused blank screen momentarily prior to Activity starting....not quite as slick. – Jon Jan 06 '15 at 22:18
3

add this flag to your activity properties.

android:launchMode="singleInstance"

Tek Yin
  • 3,011
  • 3
  • 25
  • 42
2

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!

Shlomi Fresko
  • 909
  • 11
  • 15
1

reimplemented as service - everything works as expected

user375418
  • 271
  • 1
  • 3
  • 4
1

This will bring the app back to its MainActivity even if it has opened child activities. Child activities are also cleared.

AndroidManifest:

android:launchMode="singleTop"

Code:

val intent = Intent(context, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
Metu
  • 1,033
  • 1
  • 13
  • 20
0

I used GCM example as base code for a personal project and I faced the same issue described on top. GCM Example: https://github.com/googlesamples/google-services/tree/master/android/gcm/app/src/main/java/gcm/play

I found a solution by combining to answers:

1) From @ognian answer, by adding to App Manifest on application section:

android:launchMode="singleTop"

<application
    android:allowBackup="true"
    android:icon="@mipmap/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    **android:launchMode="singleInstance">**

2) From @CommonsWare by setting setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)

private void sendNotification(String message) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
Juan Pablo
  • 1,213
  • 10
  • 15
0

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);

Happy Mittal
  • 109
  • 7
-2

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);
Jignesh Rajput
  • 3,538
  • 30
  • 50
Sushant Patekar
  • 421
  • 1
  • 5
  • 17