I am intercepting sms messages with some information in them. Then in my SmsListener I m creating notification to show in statusbar. Then, when user clicks on a notification I want
- Bring MainActivity to foreground (If such activity does not exist yet it should be created)
- Pass to it data from the sms
- Perform some ui changes basing on this data in this MainActivity
我的活动定义为
<activity
android:name=".MainActivity"
android:screenOrientation="sensor"
android:label="@string/app_name"
android:launchMode="singleTask"/>
活动启动为
Intent i = new Intent();
i.setClass(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
此外,在我的活动中,我覆盖了方法onNewActivity
@Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
// I have data from broadcast in intent variable passed to this activity
processDataFromBroadcast(intent);
}
如果MainActivity已经存在,但如果MainActivity不存在,它会启动,但未调用onNewIntent
Then I tried to invoke processDataFromBroadcast from onCreate: processDataFromBroadcast(getIntent())
.
First time data is passed correctly from my broadcast to the activity.
However if MainActivity is sent to background and then again brought to foreground either onCreate or onNewIntent is called and processDataFromBroadcast is executed again with intent sent by broadcast and thus my MainActivity is updated with data from broadcast every-time the app is bringing to foreground - the latter is unwanted, how can I make my activity to forget this intent after first handling.
Here is sample application.