开发者

Android: How to unicast an intent to an Activity with inner BroadCastReceiver?

开发者 https://www.devze.com 2023-01-11 20:25 出处:网络
I have 2 Activities that need to communicate with each other: ActivityA and ActivityB Say that ActivityA opens ActivityB. How do I unicast a message from ActivityB to ActivityA without cl开发者_如何学

I have 2 Activities that need to communicate with each other: ActivityA and ActivityB

Say that ActivityA opens ActivityB. How do I unicast a message from ActivityB to ActivityA without cl开发者_如何学Cosing ActivityB?

Here's what I tried:

  1. setResult() - but the intent will be delivered only when ActivityB closes

  2. sendBroadcast() - but this sends the intent to everyone listening to the action. I only want the action to reach ActivityA since it spawned ActivityB. I tried having ActivityA registering a BroadCast receiver, but I cannot target that via Intent's SetComponentName().

  3. startActivity() - this brings up ActivityA, which I don't want


I have 2 Activities that need to communicate with each other: ActivityA and ActivityB

You think they need to communicate with each other. I'd argue that, other than startActivityForResult()/setResult() pattern, ActivityB should not be communicating with ActivityA. Instead, they should be working through something else (e.g., service) that represents your model.

How do I unicast a message from ActivityB to ActivityA without closing ActivityB?

Use your option #2 and setPackage() to constrain the delivery.

0

精彩评论

暂无评论...
验证码 换一张
取 消