【问题标题】:Intent with old extra in onCreate() for singleTask Activity在 onCreate() 中为 singleTask Activity 使用旧额外的意图
【发布时间】:2014-08-04 04:27:37
【问题描述】:

例如,我创建了具有 2 个活动的项目:带有 android:launchMode="singleTask" 标志的 FirstActivity 和 SecondActivity。

首先用户启动 FirstActivity,然后在此 SecondActivity 将在按钮单击时启动。从 SecondActivity 我们创建状态栏通知。

Intent intent = new Intent(SecondActivity.this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(FirstActivity.ARG, true);

NotificationCompat.Builder builder = new      NotificationCompat.Builder(SecondActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher)
   .setAutoCancel(true)
   .setContentTitle("title")
   .setContentText("message");

PendingIntent notifyIntent = PendingIntent.getActivity(SecondActivity.this, 0,     intent,PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(notifyIntent);

NotificationManager notificationManager = (NotificationManager)   getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1000, builder.build());

当我点击这个通知并且我的应用程序现在正在运行时,FirstActivity#onNewIntent 方法将被执行并且意图包含 ARG extra = true。没关系。

当我点击这个通知并且我的应用程序现在没有运行时,FirstActivity#onNewIntent 方法将不会被执行,而是这个 FirstActivity#onCreate() 将被执行,我可以得到我的 ARG 标志,如 getIntent().getBooleanExtra(ARG, false) 此意图包含 ARG extra = true。这也是正确的情况。

但是当我在情况 2 按下后退按钮并再次运行应用程序后尝试退出我的应用程序时,我收到了 ARG = true 的 FirstActivity#onCreate() 意图(我处理的旧额外值)。为什么重新打开应用后我的标志为“真”?

【问题讨论】:

  • 你在onNewIntent()中使用setIntent()吗?
  • 没有。一瞬间。我试试
  • 其实不要。我认为 that 会产生这种行为。请忽略我的任何进一步的 cmets ;)
  • 但这不会解决我的问题,因为在情况 2 onNewIntent 方法不会调用,我在 onCreate() 中处理我的意图
  • 我已经复制了这个问题并得到了不同的结果。重新打开应用程序给我 ARG = false。

标签: android android-intent android-activity android-notifications android-pendingintent


【解决方案1】:

我在寻找类似问题的解决方案时遇到了这个问题。尽管这个问题是一年前的问题,但我会写下我是如何解决它的,以防它可以帮助其他人,因为我花了很多时间来解决我的问题。 我还使用 android:launchMode 将活动声明为“singleTask”。一个通知是从一个服务发送的,这个通知有一个 PendingIntent 和一个打开 MyActivity 的意图。

我的解决方案是在意图中设置标志 FLAG_ACTIVITY_NEW_TASK (http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK)。 然后创建 id 为 0 和 PendingIntent.FLAG_CANCEL_CURRENT 的 Pending Intent。

Intent _intent = new Intent(context, MyActivity.class);
_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_intent.putExtra("DATA", myData);

PendingIntent _pIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

每当单击通知时,都会在新任务中创建一个 MyActivity 实例并调用 onCreate。 Intent 包含正确的额外数据。 当我按下后退按钮并有新通知时,意图总是带来最新的额外数据。 如果 Activity 的实例已经存在(例如,当我按下主屏幕按钮时),则会调用 onNewIntent 方法,并且 Intent 会带来正确的新额外数据。

这在http://developer.android.com/guide/topics/manifest/activity-element.html#lmode 中有明确解释。 我还尝试在 PendingIntent.getActivity 和 PendingIntent.FLAG_UPDATE_CURRENT 中为每个通知使用不同的请求代码(arg 2),但我认为秘密在于将 launchMode 定义为 singleTask 和 Intent.FLAG_ACTIVITY_NEW_TASK。

我还有另一种情况,其中启动模式是“标准”。在这里,也可以通过单击从服务发送的通知来启动活动。 为了让启动的活动接收正确的额外数据,我设置了标志 FLAG_ACTIVITY_SINGLE_TOP。

_intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

此标志使 onNewIntent 方法在活动实例已经存在时被调用(例如,当我按下主屏幕按钮时)。再次使用正确的额外数据。否则,也会使用正确的额外数据调用 onCreate(例如,当我按下后退按钮时)。

Pending Intent 每次都使用新的请求代码创建,但它也应该使用相同的请求代码。我认为秘密在于启动模式和意图标志集。

PendingIntent pIntent = PendingIntent.getActivity(this, notificationRequestCode, _intent, PendingIntent.FLAG_CANCEL_CURRENT);

【讨论】:

    猜你喜欢
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2015-11-17
    相关资源
    最近更新 更多