Reputation: 543
I need to receive a broadcast event inside a broadcast receiver and than pass this information to an activity that is already open. How can I inform the activity from the broadcast receiver without it getting recreated? This causes a total refresh which is not needed.
Now I could receive intent inside a broadcast receiver that is declared within the activity, but I also need to receive the intent when its in background as well, hence the main place I am processing the intents is in a separate broadcast receiver. So I just don't know how to inform the activity that a new intent has arrived without onCreate() getting called and re-init the whole UI.
I think I need the NEW_TASK flag or it won't run.
PS: What are these insane downvotes about. What could be more relevant than how to start an activity from a broadcast receiver in such a way as not to recreate the activity. BTW, I am going to find an answer w/wo you. Why the bitter downvotes? I suspect it is because you know I could use an answer. Well I'll probably be posting an answer to this great question myself quite soon.
Upvotes: 0
Views: 534
Reputation: 543
And the answer is .....
declare the Activity in the manifest with
android:launchMode="singleTop"
Still would like feedback on this answer as I notice that in onResume() the intent does not seem to carry over the values as it did before. So I cannot pull out values that I set on the intent inside the broadcast receiver ...
Update: In order to get the values from the receiver you may need to do the following inside the Activity:
@Override
public void onNewIntent(Intent intent)
{
setIntent(intent);
}
Upvotes: 1
Reputation: 36
if you want to recreate this activity do this:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if don't want to recreate , just do this:
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
android:lauchMode= "singleTask";
Upvotes: 2