Skip to content Skip to sidebar Skip to footer

Sending Data To Another Activity That May Be Started

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 w

Solution 1:

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);

Solution 2:

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

Solution 3:

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.

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        receiver = newBroadcastReceiver(){

            @OverridepublicvoidonReceive(Context context, Intent intent) {
                Log.d("activity", "in local defined receiver");
                if(intent.getAction().equals(CustomInternalSendSmsAction) )
                {
                    handlePassedData(intent);               
                }
            }
        };
        this.registerReceiver(receiver, newIntentFilter(CustomInternalSendSmsAction));
}

Post a Comment for "Sending Data To Another Activity That May Be Started"