SETUP一个 Activity(SingleTop)从通知接收意图。意图被活动所消耗。用户点击后退按钮结束活动。 onDestory 被调用并且 isFinishing() 返回 true。长按 Home 键即可调出最近使用的应用程序。启动之前关闭的应用程序。
当用户在 Activity 上按下 Home 键后调用 onStop 时,onNewIntent 也会发生类似的情况。
Problem活动完成后重新创建时,将使用通知中的相同意图。我不想要这个。有没有办法告诉系统我们已经使用了该通知,因此停止将其提供给活动?解决办法吗?建议?
我尝试过的
- 我试图从意图中删除多余的内容。不工作。学习活动意图数据随机返回,为什么? https://stackoverflow.com/questions/5032098/clearning-activitys-intent-data-randomly-comes-back-why
- 我尝试使用 saveInstanceState() 来保留意图的时间戳。然而,当 Activity 被销毁时,onIstanceState(Bundle) 会被移除(onCreate 中为 null)。
- 我尝试在活动中将意图设置为空,但这不起作用
这个问题与这个问题非常相似:http://grokbase.com/t/gg/android-developers/119jyzf492/getintent-removeextra-in-activity-doesnt-work-for-android-launchmode-“singletask” http://grokbase.com/t/gg/android-developers/119jyzf492/getintent-removeextra-in-activity-doesnt-work-for-android-launchmode-%E2%80%9Csingletask%E2%80%9D
在我看来,这是一种解决方法,而不是解决方案。
问题
请记住,问题在于 onCreate() 和 onNewIntent() 无论如何都会给 Activity 提供相同的意图(没有什么是粘性的)。最严重的罪犯是 onCreate() 因为捆绑已保存实例状态始终为空。
解决方法
我创建了一个可序列化(让我们调用为 sFOO)类,该类保存意图操作和来自意图附加项的时间戳。在 onCreate() 期间,我加载这个序列化类 (sFOO)。在 onStart() 期间,我将 sFOO 与正在处理的意图进行比较。如果一切都是新的,我知道需要处理意图并更新 sFoo 然后保存。如果不是,我会忽略这个意图。
您需要代码吗?
在 onStart() 中
Intent activityIntent = this.getIntent();
if (activityIntent != null)
{
if (activityIntent.getAction() != null)
{
boolean newIntent = false;
//Is the intent action being processes same as previous?
if (activityIntent.getAction().compareTo(this.mLastProcessedIntent.mLastIntentProcessedAction) == 0)
{
if (activityIntent.getExtras() != null)
{
//Is the intent time stamp being processed same as previous?
if (activityIntent.getExtras().getLong(TIME_STAMP_KEY) != this.mLastProcessedIntent.mLastIntentProcessedTimestamp)
{
newIntent = true;
}
}
}
else
{
Log.d(TAG,"Last processed intent action does not equal new one.");
newIntent = true;
}
if (newIntent)
{
updateAndSaveProcessedIntent();
/*YOUR CODE HERE TO HANDLE INTENT*/
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)