【问题标题】:Android push notification unable to recreate activityAndroid推送通知无法重新创建活动
【发布时间】:2016-03-05 18:07:25
【问题描述】:

在我的应用程序中,当点击推送通知时,我无法使用新信息(捆绑数据)重新创建活动。以下是我的推送通知流程的步骤。

第 1 步: 我在单击推送通知时打开 Activity B,其中显示在 Activity B 中的信息。

第 2 步: 点击主页按钮或转到其他一些活动,以便活动 B 进入后台 (OnPause) 状态。

第 3 步: 现在,我再次通过单击带有新信息但 Activity B goes to onResume instead of onCreate state 的推送通知打开活动 B。

我的要求是它应该进入 onCreate 状态,以便我可以处理新信息。 IActivity B 如何进入 onResume 而不是 onCreate 状态?我在通知接收器类中做了以下操作。

Intent intent = new Intent(this, ListScreenActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("res_id", requestCode);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("uid", id_value);
intent.putExtras(bundle);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, requestCode, intent,0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext());
Notification notification = mBuilder
         .setSmallIcon(R.mipmap.ic_launcher)
         .setTicker(tickerMessage)
         .setWhen(0)
         .setAutoCancel(true)
         .setContentTitle(app_name)
         .setStyle(new NotificationCompat.BigTextStyle().bigText(tickerMessage)
         .setContentIntent(resultPendingIntent)
         .setContentText(tickerContent).build();

NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(), notification);

注意: 这里ListScreenActivity (Activity B) is not Launcher activity

【问题讨论】:

    标签: android android-intent notifications


    【解决方案1】:

    您无法更改 Android 活动生命周期。您应该期望每次都会发生同样的事情。 onCreate 每个活动对象的实例仅调用一次。这是无法改变的。您可以阅读activity lifecycle 以获取更多信息。

    【讨论】:

      【解决方案2】:

      如果您想关闭现有的活动堆栈而不管那里有什么并创建新的根,正确的标志集如下:

      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
      

      【讨论】:

        猜你喜欢
        • 2016-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多