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
My activity is defined as
<activity
android:name=".MainActivity"
android:screenOrientation="sensor"
android:label="@string/app_name"
android:launchMode="singleTask"/>
Activity is launched as
Intent i = new Intent();
i.setClass(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Also in my activity I have overridden method onNewActivity
@Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
// I have data from broadcast in intent variable passed to this activity
processDataFromBroadcast(intent);
}
It works fine if the MainActivity already exists but if MainActivity does not exist it is started however onNewIntent was not called
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.
Activity or dialog appears in foregroundWhen the covered activity returns to the foreground and regains focus, it calls onResume() . If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state.
A foreground service executes an action that is visible to the user. A foreground service, for example, would be used by an audio app to play an audio track. A single, concentrated item that the user may accomplish is referred to as an activity. A Notification must be displayed by foreground services.
Running in the Foreground means your app is currently Fully Visible on your device, you can see it and interact with it and it will respond to you right away.
For an activity to launch only one instance of itself, have a look at the <activity>
manifest element, and particularly android:launchMode
. You want to configure it with either singleTask
or singleInstance
.
To pass data to your activity, you add data to the Intent
you use to open it. To pass data with the intent, use the putExtra()
methods of the intent before sending it off, and getExtra()
methods to retrieve them in your receiving activity.
I'm assuming that you know roughly how intents work, but if not you could learn more about intents by taking a look at this Android developers article.
in case your problem is still unresolved, as I was just running into the same issue, here's how I solved it:
I am putting a timestamp as intentId as an extra upon the intent during it's creation. the first time, I am handling the intent in onCreate()
or onNewIntent()
I am reading the intentId and store it as the last intent handled. so the next time onCreate()
or onNewIntet()
is invoked I can check the intentId and if it equals the id of the last intent handled, I ignore it! It don't know if this helps in your case, maybe you can adopt it.
To keep intentId independent from activity lifecycles you could persist it in the userdefaults.
I agree that one would expect calling setIntent(new Intent())
in onNewIntent
should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With