繁体   English   中英

当我单击通知并进入活动时,为什么调用onDestroy()?

[英]Why is onDestroy() called when I click on a notification and go to my activity?

当我单击通知并进入活动时,为什么调用onDestroy()? 控制流程如下所示-> onCreate-> onStart-> onResume-> onDestroy。 以下是我添加通知的代码:

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()方法:

@Override
protected void onDestroy() {
    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;
}

只需在通知中传递一些putExtra字符串,例如

if (roomId.contains("admin1")) {
        notificationIntent = new Intent(this, DemoChatActivity.class);
    } else {
        notificationIntent = new Intent(this, copyDemoChatActivity.class);
    }
notificationIntent.putExtra("From","Notification");

在您的Activity中,在实现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");
      }

而在onDestroy方法中

    @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;
    }

如果有任何问题,请通知我。

暂无
暂无

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

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