When you click on the Android notification, a new action is created instead of moving to the existing one.

My application has one running action that creates a notification. When a notification is selected, instead of switching to the current activity, the launched activity is destroyed and a new action is created - IN ANDROID 3.0 AND ABOVE. How to prevent this?

This question has been answered many times, mainly by specifying the flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP, and launchMode = "singleTop" in the manifest. But most of the answers were before Android 3.0, so what else is needed to fix this in newer versions of Android?

The following simple test application demonstrates my problem:

NotifyTest.java:

package com.example.notifytest;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

public class NotifyTest extends Activity {
    private static int nextCode = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify_test);

        int notifyCode = nextCode++;

        Intent notIntent = new Intent(this, NotifyTest.class);
        notIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        TaskStackBuilder stkBuilder = TaskStackBuilder.create(this);
        stkBuilder.addParentStack(NotifyTest.class);
        stkBuilder.addNextIntent(notIntent);

        NotificationCompat.Builder notBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("NotifyTest")
            .setContentText("notification #" + notifyCode)
            .setContentIntent(stkBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));

        NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(notifyCode, notBuilder.build());
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notifytest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.notifytest.NotifyTest"
            android:label="@string/app_name"
            android:launchMode="singleTop" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

, 3.0 1.6, , Froyo. , onCreate , .

3.0 , , Jelly Bean. onDestroy onCreate , , .

, : - onCreate onDestroy - . ( onCreate) - , .

!

+4
1

. / API 22/23 API 17/ 4.2, 3.0 , Altering , . "singleTask", .

    <activity
        android:name=".MainActivity"
        android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
+2

Source: https://habr.com/ru/post/1523416/


All Articles