Reputation: 203
I'm trying to add more flags at an Intent for launching a new Activity inside a BroadcastReceiver, responding to a particular intent sent from another part of the app.
I've added as I noticed by the LogCat messages the FLAG_ACTIVITY_NEW_TASK
flag at the Intent I created and then other ones but I get the same error in LogCat as the FLAG_ACTIVITY_NEW_TASK
was not there.
Here's the code:
public class actReceiver extends BroadcastReceiver {
...
public void onReceive(Context context, Intent intent) {
...
else if (intent.getAction().equals("something")) {
Intent prefAct = new Intent(context, PreferencesActivity.class)
prefAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(prefAct);
}
... }
... }
And the particular LogCat error:
Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Upvotes: 3
Views: 4803
Reputation: 19
simple way of doing it with kotlin and anko see snippet see example snapshot
startActivity(intentFor().clearTask().newTask().noHistory())
hope this helps
Upvotes: 0
Reputation: 157457
setFlags
just assign the argument, as you can see from the snippet
public Intent setFlags(int flags) {
mFlags = flags;
return this;
}
so in your case you are just assign the last one. To fix it put it on or
prefAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_SINGLE_TOP);
or use addFlags
which does the same thing
public Intent addFlags(int flags) {
mFlags |= flags;
return this;
}
Upvotes: 5
Reputation: 8774
use addFlags()
instead of setFlags()
to add additional flags.
http://developer.android.com/reference/android/content/Intent.html#addFlags(int)
setFlags()
sets the complete set of flags to be used, so you would have to |
all flags to be used in here. addFlags()
can be called multiple times, and does the |
of flags for you.
Upvotes: 2