BollMose
BollMose

Reputation: 3498

android :how to communicate between activities, or how to hold the instance of each other

I have two activities A and B, B is a dialog mode , A start B, both them can visible. During both activities running, how to communicate each other? one method is register BroadcastReceiver at each activity. but BroadcastReceiver can't get result of each sendBroadcast(i): it is safe:

        // at ActivityA onCreate
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                ...//receive msg
            }
        };
        Log.i(TAG,"register broadcast");
        IntentFilter intentFilter = new IntentFilter(
                "xxx1"); //"xxx2" in ActivityB
        this.registerReceiver(mReceiver, intentFilter);

and when some happened at ActivityB:

        Intent i = new Intent("xxx1"); // "xxx2" at activityA
        ...//some paramters
        sendBroadcast(i);

        Log.i(TAG,"send status");

Upvotes: 0

Views: 188

Answers (3)

Pradip
Pradip

Reputation: 3177

Can you please trying by using FragmentActivity. Activity A is your main FragmentActivity and Activity B is Fragment display Activity B as and Use Static Variable to interchange values. I'm not sure about it.

Upvotes: 0

Infinite Recursion
Infinite Recursion

Reputation: 6557

Both activities are NOT running simultaneously.

When A starts B, the sequence will be as below( I have ommited other lifecycle events which depend on the individual code of your activities):

onPause() of A [foreground lifetime ends for A]
onResume() of B [foreground lifetime starts for B]
...B ends...
onPause() of B [foreground lifetime ends for B]
onResume() of A [foreground lifetime starts for A]

(since B is a dialog which partially covers A). When B finishes, onResume() of A gets invoked.

When A starts B, the foreground lifetime of A finishes, but visible lifetime of A continues because A is still partially visible (in background of B).

So for your question During both activities running, how to communicate each other?
Answer: At any point of time, only one activity is running (i.e has foreground lifetime and can interact with the user). Both activities are NOT running simultaneously.

The Documentation clearly mentions:

The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user.

The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user.

Upvotes: 1

Michael Barany
Michael Barany

Reputation: 1669

You don't want to hold on to the instance of the Activity because it can cause pretty bad memory leaks. Activities can be recreated on any config change such as device rotation or keyboard change, and in this case a new Activity instance will be created and your reference will be for the old activity instance.

There's a few things you can do to exchange data with activities. (In order from best/easiest - to more difficult)

1) If it's a simple one way data exchange you can send a Bundle of data when starting an Activity. See (http://developer.android.com/reference/android/content/Intent.html)

2) You can use startActivityForResult which will open an Activity for the purpose of returning some result of data. See (http://developer.android.com/reference/android/app/Activity.html)

3) A bit more complicated, but you can use an EventBus mechanism with pub/sub behavior like Otto (http://square.github.io/otto/)

Upvotes: 1

Related Questions