简体   繁体   English

为什么在我开始通知时会调用onDestroy()? 怎么预防呢?

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

I have two Activities, ChatActivity and ChatOneActivity 我有两个活动, ChatActivityChatOneActivity

I want to build a notification in the ChatActivity ,and when I press the notification, I found the onDestroy() method is called. 我想在ChatActivity构建一个通知,当我按下通知时,我发现调用了onDestroy()方法。 Why? 为什么? I can understand why the onStop method getting called, because the view is no longer visible. 我可以理解为什么调用onStop方法,因为视图不再可见。 But onDestroy() ? 但是onDestroy() Why? 为什么? I didn't finish the ChatActivity ! 我没有完成ChatActivity

How do I prevent it? 我该如何预防呢? I just want the onStop method to get called, I don't want the Activity to get killed. 我只是希望onStop方法,我不希望Activity被杀死。

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>

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. TaskStackBuilder.create将返回一个新的TaskStackBuilder,用于启动由一系列活动组成的新任务堆栈。

Replacing them with a regular pendingintent like this solved it: 用这样的常规pendingintent替换它们解决了它:

PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM