Today there is a problem of communication between acitivity, because we are wrapping the activity with tabactivity (what we call a), so the two activity is at the same time. When you start other activities in tabactivity to handle some business, and when the business process is complete, the results are returned through Onactivityresult (), we need to update the a based on the returned results, but now the problem is, how do we update a?
I found that Tabactivity's Onactivityresult () was executed before the onresume of a, that is, I can do some processing in tabactivity, update the state when a executes onresume, The first thing I think about is that through sharedpreference, by writing an identity, let a to read the identity. But this is obviously not satisfying, and it's not a good idea to write a file to pass the state.
Alternatively, we can transmit the status via broadcast, but the broadcast is too heavy, and as an app, this system-level operation should not be done, and system-level broadcasts can also bring security issues. Once captured by other applications, it can be exploited.
Finally, we learned from colleagues that there is something that can help us solve this problem: Localbroadcastmanager.
It comes from Android's support package, The class name is Android.support.v4.content.LocalBroadcastManager, which is used to send broadcast between different components within the same application, just in accordance with our scene. At the same time, it sends the broadcast only in the app spreads, does not leak to other applications, the security also has the guarantee. And it's very simple to use, similar to a normal broadcast.
Use the following method, which is called by the receiver, which is our A.
? ? Broadcastreceiver camerapicbroadcastreceiver = new Broadcastreceiver () {
@Override
public void OnReceive (Contextcontext, Intent Intent) {
Boolean issuccess = Intent.getextras (). Getboolean (mconstants.key_login_sucess);
? ? ? ? ? ?.......//Update status
}
};
intentfilter intentfilter = new Intentfilter ( mconstants.key_intent_login_result);
final Localbroadcastmanager Localbroadcastmanager = Localbroadcastmanager. getinstance (CTX);
Localbroadcastmanager.registerreceiver (camerapicbroadcastreceiver, intentfilter);
The sender of the broadcast has the following wording:
? ? ? ? ? Intent loginsucessintent = new Intent ();
Loginsucessintent.setaction (Mconstants.key_intent_login_result);
Loginsucessintent.putextra (mconstants.key_login_sucess, false);
Localbroadcastmanager.getinstance (context). Sendbroadcast (loginsucessintent);
Localbroadcastmanager is a singleton, and it can be used in a simple way, and it solves the interaction problem between service and activity.
Android in Localbroadcastmanager use