In Android development, we use method callbacks in many places. Callbacks are a mechanism to separate the definition and function of the method. The purpose is to understand that its essence is based on the observer design pattern, that is, a simplified version of the observer design pattern, such as: progress callbacks during download, callbacks between adapter and activity, callbacks between Javabean and fragment and fragment, etc. The purpose of callbacks is mainly two: one is to pass data, and the other is to keep data updated synchronously. There are two common forms: one is to use the internal class form to obtain the subclass object of the interface, and the other is to directly implement the defined interface.
1. Internal form
1. Define an interface at the end where data needs to be passed, including some methods and parameters that need to be listened to in the interface.
2. Define an interface type variable to store data.
3. Create a public method, let external calls, and pass parameters of interface type to initialize the data of the interface type it defines.
/** * Define an interface */ public interface onListener{ void OnListener(String code,String msg); } /** *Define a variable to store data */ private onListener listener; /** * Provide a common method and initialize the interface type data */ public void setListener( onListener listener){ = listener; }
4. Call the methods inside the interface at the appropriate location to pass data.
/** * Call the interface to it at the appropriate location and assign it a value */ if (listener!=null) { (rtncode,rtnmsg); }
5. Create an object adjustment method where you need to obtain data.
Print print = new Print(); (new () { @Override public void OnListener(String code, String msg) { // Get the data here for processing } });
2. Implement the form of interface
1. Define an interface, which can be named again, or defined in a class.
/** * Define an interface */ public interface onListener{ void OnListener(String code,String msg); }
2. Initialize the interface at the construction method on the end that needs to be passed.
public Print(OnListener listener) { = listener; }
3. Call the methods inside the interface at the appropriate location to pass data.
/** * Call the interface to it at the appropriate location and assign it a value */ if (listener!=null) { (rtncode,rtnmsg); }
4. Create object passing parameters where data needs to be retrieved.
Print print = new Print(this);
This refers to the activity of the current page. If it is a fragment, you need to rewrite the onAttach method to initialize it, and then cast the type conversion to obtain the interface object. At this time, the interface is passed in the construction method to obtain the data listener, choose one of the following two.
@Override public void onAttach(Activity activity) { (activity); OnListener listener = (OnListener) activity; } @Override public void onAttach(Context context) { OnListener listener = (OnListener) activity; }
Note: Directly obtaining the activity where the current fragment is located in the fragment, calling getActivity for forced rotation or direct transmission will result in an error. The error reported is a type conversion error.
5. Let activity implement this interface, rewrite its abstract method, and handle tasks in the abstract method.
@Override public void OnListener(String rtncode, String rtnmsg) { // Get the data here for processing}
The above two callback methods are suitable for data transfer between two classes. Let’s take a look at data transfer between three classes, javabean—>activity—>fragment. Requirements: Get data in javabean. When clicking the fragment button, the obtained data is required to be displayed for related business processing. The steps are as follows:
1. Create a new package, define an interface, and define related methods.
2. Initialize the interface in the javabean constructor and call the methods in the interface in the appropriate location.
3. Rewrite onAttach in fragment, initialize the interface, and force it to interface type.
4. Create an object and pass the interface type with strong rotation in parameter 3.
5. Let the activity implement the interface, rewrite the abstract method, and process the data in the method.
The specific code is omitted...
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!