Personal Understanding of callback requests in android, android callback
The Fragment class provides the onCreateOptionMenu (Menu, MenuInflater) callback function for "option Menu" management. You can call this function to create "option Menu ".
When the "option Menu" is required, Android-OS calls the Fragment onCreateOptionMenu (Menu, MenuInflater) of Fragment through the FragmentManager of the Activity to create it.
When you click "option menu", Fragment receives a callback request from the onOptionItemSelected (MenuItem) method.
// The option menu can contain multiple menu items. By checking the menu item ID, you can determine which menu item is to be taken.
What is a callback request ?????
From the user's perspective, when I click "option menu", one thing will happen (this app jumps to another activity ).
The Code overwrites onOptionItemSelected (MenuItem) {// redirects to another activity} in fragment }.
Let's take a look at an example of a classic callback function.
Simulation 911 event-Result: WTC (tower of Hangzhou) was bombed
1. interfaces, callers, and executors
// Equivalent to the interface InApublic interface BoomWTC {// get the decision of bin Laden public benLaDengDecide (); // execute the bombing of world trade public void boom ();} // equivalent to class Apublic class At $911 implements BoomWTC {// equivalent to [Background 1] private boolean decide; private TerroristAttack ta; // equivalent to [Background 2] public At $911 () {Date now = new Date (); SimpleDateFormat myFmt1 = new SimpleDateFormat ("yy/MM/dd HH: mm"); this. dicede = myFmt. format (dt ). equals ("01/09/11"); this. ta = new TerroristAttack ();} // get Bin Laden's decision public boolean benLaDengDecide () {return decide;} // execute the bombing of world trade public void boom () {ta. attack (new At $911); // class A calls the class B method to pass in its own object, equivalent to [you call me]} // equivalent to class Bpublic class TerroristAttack {public TerroristAttack () {} public attack (BoomWTC bmw) {-- this is equivalent to [Background 3] if (bmw. benLaDengDecide () {/* class B calls back class A's method in the method, which is equivalent to [I call you back ]*/
}}}
2. Here is the main function
public class $911{ public static void main(String[] args){
At$911 $911=new At$911();
$911.boom();
} }
Process: Create a 911 instance in main () and call the boom () method. This method calls the attack () method of the TerroristAttack instance in instance 911. This method creates a 911 anonymous instance, input attack ().
TerroristAttack's attack (BoomWTC bmw) accepts an instance implementing the BoomWTC interface and uses the interface variable bmw to reference the incoming 911 anonymous instance. Then, bmw. benLaDengDecide () calls the benLaDengDecide () method of 911, returns a true value (assuming Today is July 22, September 11), and then uses if to judge and perform bombing...
Comparison:
AndroidOS |
Main |
New activity. onMenuItemSelected |
New 911. boom |
Fragment. onOptionItemSelected |
TerroristAttack. attack |
Therefore, when you click "option menu", Fragment will receive a callback request for the onOptionItemSelected (MenuItem) method, that is, androidOS will eventually call onOptionItemSelected () response-click the option menu-this event