如何强制从意图重新启动活动重新启动、意图

2023-09-07 10:44:15 作者:无休无止っ

我的 MainActivity lauchMode =singleTask

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

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

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

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

 意向意图=新的意图();
intent.setClass(这一点,MainActivity.class);
intent.putExtra(triggerid,triggerid);
startActivity(意向);
 
android中用意图打开音乐播放器并播放指定路径的音乐

解决方案

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().