Use of LocalBroadcastManager in android, androidlocal
Today, we have encountered A problem of communication between acitivity. Because we use TabActivity to wrap Activity (we call it A), these two activities exist at the same time. When other activities are started in TabActivity to process some businesses, onActivityResult () is used to return the processing results when the business processing is complete. We need to update A Based on the returned results, but now the question is, how can we update?
I found that the onActivityResult () of TabActivity is executed before onResume of A. That is to say, I can perform some processing in TabActivity and update the status when onResume is executed by, the first thing I think of is to write an identifier through SharedPreference so that A can read the identifier. However, this method obviously does not satisfy us. It is not a good practice to pass the state by writing a file.
Alternatively, we can transmit the status through broadcasting. However, broadcast operations are too heavy. As an app, we should not do such system-level operations, and system-level broadcast also brings security problems. Once captured by other applications, it may be exploited.
Finally, I learned from my colleagues that this problem can be solved for us: LocalBroadcastManager.
It comes from the android support package and the class name is android. support. v4.content. localBroadcastManager is used to send Broadcast messages between different components in the same application, which is exactly consistent with our scenario. At the same time, the broadcast sent by it is only transmitted in the app and will not be leaked to other applications. The security is also guaranteed. In addition, it is easy to use, similar to normal broadcast.
The usage is as follows: in the receiver, that is, our A, call this method.
BroadcastReceiver cameraPicBroadcastReceiver = new BroadcastReceiver (){
@ Override
Public void onReceive (Contextcontext, Intent intent ){
Boolean isSuccess = intent. getExtras (). getBoolean (MConstants. KEY_LOGIN_SUCESS );
... // Update the status
}
};
IntentFilter intentFilter = new IntentFilter (MConstants. KEY_INTENT_LOGIN_RESULT );
Final LocalBroadcastManager localBroadcastManager = LocalBroadcastManager. getInstance (ctx );
LocalBroadcastManager. registerReceiver (cameraPicBroadcastReceiver, intentFilter );
The Broadcast Sender is written as follows:
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 instance. It is easy to use and can solve the Interaction Problem between services and activities.
What is the essential role of Local Service in Android?
Where does the Local Service come from?
Android cannot use local storage? Why is onerror always triggered?
Webview must be enabled