For an example of how to use the Listener Listener mode:
Background: In one activity there is a way to get a dialog up, which is the "Loading" dialog box, in which the system is dealing with something,
But the result is in another manager, but canceling the dialog display is only available in my activity, and the question is: How to get the manager to the results,
Send me a message to cancel the display of dialog in the activity.
Let's use the listener mode to implement it.
First, I need to cancel the dialog display, if the method is DismissDialog (), and this method must be used somewhere in the activity.
Then we can. This method is placed in the listener implementation method, which is equivalent to the Click () method in the Onclicklistener.
You must have the following code snippet in your activity:
Accountmanager Maccountmanager = Accountmanager.getinstance ();
Maccountmanager.seterroroccurlistener (New Onerroroccurlistener () {
@Override public
void Onerroroccur () {
DismissDialog (dialog_loading_cancelable);
}
});
Second: From the above code can be seen, there is a interface called Onerroroccurlistener, he has a method is not implemented called onerroroccur (), so we need to write a interface interface,
It doesn't matter where you put it, but it must be public. As follows:
Public interface onerroroccurlistener{public
void Onerroroccur ();
}
Again: As can be seen in the above code, the manager has a Seterroroccurlistener (Onerroroccurlistenr listener) method, so in the manager to add
public void Seterroroccurlistener (Onerroroccurlistener listener) {
This.mdialoglistener = listener;
}
Also set the attribute Mdialoglistener in the manager as a global variable.
Finally: and the most important question is: how to trigger the activity of DismissDialog () to execute it.
In the result method in your manager class, add
public void result (int error) {
if (mdialoglistener! = null) {
mdialoglistener.onerroroccur ();
}
Toast.maketext (Healthapplication.getcontext (), error, Toast.length_long). Show ();