Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why onDestroy() is get called when I start a notification? How to prevent it?

Tags:

android

I have two Activities,ChatActivity and ChatOneActivity

I want to build a notification in the ChatActivity,and when I press the notification, I found the onDestroy() method is called. Why? I can understand why the onStop method getting called, because the view is no longer visible. But onDestroy()? Why? I didn't finish the ChatActivity!

How do I prevent it? I just want the onStop method to get called, I don't want the Activity to get killed.

Here is how I build the notification

private void showNotification(String id, String message) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            getApplicationContext())
            .setSmallIcon(android.R.drawable.sym_action_email)
            .setContentTitle("You have a new message")
            .setContentText(message);
    Intent intent = new Intent(ChatActivity.this, ChatOneActivity.class);
    intent.putExtra("toId", id);
    TaskStackBuilder stackBuilder = TaskStackBuilder
            .create(ChatActivity.this);
    stackBuilder.addParentStack(ChatOneActivity.class);
    stackBuilder.addNextIntent(intent);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_CANCEL_CURRENT);

    builder.setContentIntent(pendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, builder.build());
}

And also my manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webmobilegroupchat"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.webmobilegroupchat.MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.example.webmobilegroupchat.ChatActivity"
        android:label="@string/title_activity_chat" >
    </activity>
    <activity
        android:name="com.example.webmobilegroupchat.ChatOneActivity"
        android:label="@string/title_activity_chat_one" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.webmobilegroupchat.ChatActivity" />
    </activity>
    <activity
        android:name="com.example.webmobilegroupchat.SplashActivity"
        android:label="@string/title_activity_splash" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

like image 698
Xiang.G Avatar asked Nov 08 '22 09:11

Xiang.G


1 Answers

Because you called this code:

TaskStackBuilder stackBuilder = TaskStackBuilder.create(ChatActivity.this);
stackBuilder.addParentStack(ChatOneActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_CANCEL_CURRENT);

TaskStackBuilder.create will return a new TaskStackBuilder for launching a fresh task stack consisting of a series of activities.

Replacing them with a regular pendingintent like this solved it:

PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
like image 193
Jimson Avatar answered Nov 15 '22 06:11

Jimson