I have a BroadcastReceiver that I'm using to send data to another activity which may or may not be running. I'm using the intent in my onReceive() method, and putting the data in with putExtra(). THe data gets sent to the activity, however, the activity's onCreate() method gets called even though the activity is already running and in the foreground, so I guess it's creating a new instance. I want to only have onResume() called, or perhaps there's some other way I can create/start the intent if it doesn't exist, and if it does, just have it 'resumed'. Right now, the activity is being recreated, and I do not want this.
public void onReceive(Context context, Intent intent) {
intent.setClass(context, MyActivity.class);
intent.putExtra("message", "the data here");
//intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d("sending msg", "msg");
context.startActivity(intent);
}
If I do not use FLAG_ACTIVITY_NEW_TASK, a RuntimeException is throw, specifically telling me that if I want to start an activity from something that is not an activity, I must use FLAG_ACTIVITY_NEW_TASK
.
If you want to bring your possibly running activity to the front, and finish any other activities that may be on top of it, you can specify the following flags:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
In this case, when your activity that receives this intent is already running, the onNewIntent()
method will be invoked, rather than recreating it from scratch.
If you want to simply reorder the activities when your desired activity is running (i.e. move it to the top of the stack), try the following:
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i think you should add android:launchMode="singleTask" to your activity declaration on your manifest or start the activity intent with the flag FLAG_ACTIVITY_NEW_TASK which does exactly the same thing as singleTask
in a case where the activity already exist somewhere in the stack then it is brought to the front with a call to onNewIntent() instead of onCreate().
do a readup on the different launch modes on. very important fundamentals! :D
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
http://developer.android.com/guide/topics/manifest/activity-element.html
I ended up rebroadcasting (internally) the parsed information that my BroadcastReceiver was getting in. The actvity, in code, registered for the event and had a small handler method in a private-created receiver. This was a good solution as I know that this activity would always be running in my case.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Log.d("activity", "in local defined receiver");
if(intent.getAction().equals(CustomInternalSendSmsAction) )
{
handlePassedData(intent);
}
}
};
this.registerReceiver(receiver, new IntentFilter(CustomInternalSendSmsAction));
}