Android 为什么在单击通知并转到“活动”时调用onDestroy()?
为什么在单击通知并转到“活动”时调用onDestroy()?控制流程如下->onCreate->onStart->onResume->onDestroy。以下是我添加通知的代码:-Android 为什么在单击通知并转到“活动”时调用onDestroy()?,android,android-notifications,Android,Android Notifications,为什么在单击通知并转到“活动”时调用onDestroy()?控制流程如下->onCreate->onStart->onResume->onDestroy。以下是我添加通知的代码:- private void addNotification(String message, User user, String roomId) { System.out.println("Inside addNotif service"); if (message.length()==0) {
private void addNotification(String message, User user, String roomId) {
System.out.println("Inside addNotif service");
if (message.length()==0) {
return;
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.notification_icon);
builder.setContentTitle("2222----You have new message(s)");
builder.setContentText(message);
builder.setAutoCancel(true);
Intent notificationIntent;
if (roomId.contains("admin1")) {
notificationIntent = new Intent(this, DemoChatActivity.class);
} else {
notificationIntent = new Intent(this, copyDemoChatActivity.class);
}
//notificationIntent.putExtra(com.clover_studio.spikachatmodule.utils.Const.Extras.USER, user);
Config config = new Config(com.clover_studio.spikachatmodule.utils.Const.Api.BASE_URL, com.clover_studio.spikachatmodule.utils.Const.Socket.SOCKET_URL);
notificationIntent.putExtra(com.clover_studio.spikachatmodule.utils.Const.Extras.CONFIG, config);
notificationIntent.putExtra(com.clover_studio.spikachatmodule.utils.Const.Extras.ROOM_ID, roomId);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
我的onDestroy()方法:
只需在Notiification中传递一些额外的字符串,如
if (roomId.contains("admin1")) {
notificationIntent = new Intent(this, DemoChatActivity.class);
} else {
notificationIntent = new Intent(this, copyDemoChatActivity.class);
}
notificationIntent.putExtra("From","Notification");
在活动中,如果实现了onDestory()
方法,请维护一个标志
boolean isFromNotification = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String tmString = intent.getExtra().getString("From");
isFromNotification = tmString.equalsIgnoreCase("Notification");
}
在ondestory
方法中
@Override
protected void onDestroy() {
if(!isFromNotification ){
System.out.println("copyDemoChat - onDestroy()");
super.onDestroy();
Log.d(TAG, "unregistering reciever");
// Unregister since the activity is about to be closed.
Intent intentN = new Intent(copyDemoChatActivity.this, com.clover_studio.democloverapp.Utils.SSNotificationService.class);
copyDemoChatActivity.this.startService(intentN);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
isRunningCDCA = false;
}
isFromNotification =false;
}
如果有任何问题,请告诉我。此代码可能会创建新活动。所以以前的将被摧毁。。您可以在现有活动中的
onNewIntent()
中获得新的意图。这让我想到了。但遗憾的是,即使在实现了onNewIntent()方法之后,仍然调用了onDestroy()函数。好的,因此我的onNewIntent()方法并不仅仅被调用!如果我将要进行的活动已经出现在后台,如何确保调用它?我可以知道您在onDestroy
方法中做了什么吗?@MayurRaval-编辑问题以显示onDestroy()方法“如果”的结尾在哪里?@ShraddheyaShendre,很抱歉代码错误,刚刚编辑成功!必须做一些更改,比如在“equalsIgnoreCase”之前,必须添加一个检查字符串是否为null的检查,并且必须从“if”中去掉super.ondestory(),否则会引发异常。当我运行此条件时,我会遇到SuperNotCalledException。你有什么办法解决这个问题吗?@Alon,这个错误可能来自ondestory()
方法。。您可以通过放置super.ondestory()来尝试它代码>超出if
条件。
@Override
protected void onDestroy() {
if(!isFromNotification ){
System.out.println("copyDemoChat - onDestroy()");
super.onDestroy();
Log.d(TAG, "unregistering reciever");
// Unregister since the activity is about to be closed.
Intent intentN = new Intent(copyDemoChatActivity.this, com.clover_studio.democloverapp.Utils.SSNotificationService.class);
copyDemoChatActivity.this.startService(intentN);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
isRunningCDCA = false;
}
isFromNotification =false;
}