![](/img/trans.png)
[英]Why is onDestroy() called when I click on a notification and go to my activity?
[英]Why onDestroy() is get called when I start a notification? How to prevent it?
我有兩個活動, ChatActivity
和ChatOneActivity
我想在ChatActivity
構建一個通知,當我按下通知時,我發現調用了onDestroy()
方法。 為什么? 我可以理解為什么調用onStop
方法,因為視圖不再可見。 但是onDestroy()
? 為什么? 我沒有完成ChatActivity
!
我該如何預防呢? 我只是希望onStop
方法,我不希望Activity被殺死。
以下是我如何構建通知
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());
}
而且我的表現
<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>
因為您調用了此代碼:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ChatActivity.this);
stackBuilder.addParentStack(ChatOneActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_CANCEL_CURRENT);
TaskStackBuilder.create
將返回一個新的TaskStackBuilder,用於啟動由一系列活動組成的新任務堆棧。
用這樣的常規pendingintent替換它們解決了它:
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.