如何强制从意图重新启动活动 [英] how to force restart activity from intent

查看:160
本文介绍了如何强制从意图重新启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 MainActivity lauchMode =singleTask

现在我想从有特殊意图数据的通知开始活动。 在 MainActivity.onResume 我访问给定的意图数据...

现在的问题是:当活动已经存在了,我点击的通知,该活动涉及到的前景,但该方法 onResume 不叫,我无法访问意图数据。

我试过标志 FLAG_ACTIVITY_CLEAR_TASK ,这适用于蜂窝而不是姜饼。

这是怎么开始的活动从通知:

 意向意图=新的意图();
intent.setClass(这一点,MainActivity.class);
intent.putExtra(triggerid,triggerid);
startActivity(意向);
 

解决方案

onResume()总是被调用,如果该活动是不是在前台。然而,你可能看到的是, getIntent()将返回启动该活动,而不是最近一次发送给它的意图的意图。

要解决这个问题,你应该重写 onNewIntent()。这将接收发送给它的新意图。然后,你可以调用 setIntent()与接收到的意图,这将导致 getIntent()返回新意图时,在 onResume使用()

My MainActivity has the lauchMode="singleTask"

Now I want to start the activity from a notification with special intent data. in MainActivity.onResume I access the given intent data...

The problem is: When the activity already exists, and I click on the notification, the activity comes to foreground, but the method onResume is not called and I cannot access the intent data.

I tried the flag FLAG_ACTIVITY_CLEAR_TASK and this works for Honeycomb but not for Gingerbread.

This is how I start the activity from a notification:

Intent intent = new Intent();
intent.setClass(this, MainActivity.class);
intent.putExtra("triggerid", triggerid); 
startActivity(intent);

解决方案

onResume() is always called if the activity is not in the foreground. However, what you're probably seeing is that getIntent() is returning the intent that started the activity, not the intent that was most recently sent to it.

To fix this, you should override onNewIntent(). This will receive the new intent sent to it. Then you can call setIntent() with the received intent, which will cause getIntent() to return the new intent when used in onResume().

这篇关于如何强制从意图重新启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆