Callback in Android?

前端 未结 3 1178
终归单人心
终归单人心 2020-12-08 15:20

In Android application development, I frequently go through the word CallBack in many places. I want to know what it means to tell us technically - and how I ca

相关标签:
3条回答
  • 2020-12-08 15:50

    A Callable interface can be used to run a piece of code as Runnable does. However, Callable can return the result and can throw checked an exception.

    For more detail. http://developer.android.com/reference/java/util/concurrent/Callable.html

    By using Callable interfaces you can pass an argument as function I added a simple code snippet for understanding.

    public class MainActivity<V> extends Activity {
    
    
        Callable<String> doLogin=null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            doLogin=new Callable<String>() {  //created but not called now.
    
                @Override
                public String call() throws Exception {
    
                    //make some piece of code
                    return "something"; //or false
                }
            };
    
            CheckSession checkSession=new CheckSession("sessionName");
            String sessionKey="";
    
    
            try {  //we are sending callable to the DAO or any class we want 
                 sessionKey=checkSession.getSessionKey(doLogin);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    
    
    public class CheckSession{
    
    
        String sessionName="";
        Callable<String> func=null;
    
        public CheckSession(String sessionName) {
            super();
            this.sessionName = sessionName;
    
        }
    
        public String getSessionKey(Callable<String> doLogin) throws Exception{
    
            func=doLogin;
    
            return (String) func.call();
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-08 15:54

    i want to know what it means, tell us technically

    http://en.wikipedia.org/wiki/Callback_%28computer_science%29

    "In object-oriented programming languages without function-valued arguments, such as Java, [callbacks] can be simulated by passing an abstract class or interface, of which the receiver will call one or more methods, while the calling end provides a concrete implementation. Such objects are effectively a bundle of callbacks, plus the data they need to manipulate. They are useful in implementing various design patterns such as Visitor, Observer, and Strategy."

    how i can manage the callback of the applications

    I have no idea what this means.

    0 讨论(0)
  • 2020-12-08 15:55

    Hmm. How about an example. You write a quicksort algorithm in C. The user who wants to use your algorithm must supply a compare method appropriate for what the user is sorting with your algorithm. The user must pass a function pointer to the user's compare method to your quicksort code. The quicksort code uses this address, the function pointer, to CALL BACK to the user's compare function. You provide a function prototype, no implementation, since you cannot possibly know how to determine the ordinality of what is being sorted. The user supplies the implementation of compare that makes sense for what the user is sorting. This implementation must match the function prototype. The function pointer is used by the quicksort alogorithm to reach back and touch the user's code.

    This is actually about polymorphism.

    In java, you can use an interface to do this. So for sorting, see the interface IComparer and IComparable.

    0 讨论(0)
提交回复
热议问题