2

I have read many answers and referred books but i don't understand the term "callback methods". For example onCreate() is a callback method. Can anyone explain form scratch. Thank you.

chris
  • 194
  • 2
  • 10
  • 1
    Should be fairly simple to find an [answer](https://www.quora.com/What-are-callback-methods-in-Java) – Vucko Feb 09 '19 at 12:28
  • 1
    Basically a callback is just a block of code you want to be called after something else. As @Erik said, onCreate() is just a lifecycle method. So this would mean that the onCreate() will "call after" the activity has been created. So it would literally be "upon creation do this..." If you are interested in knowing more about callbacks I explained it here in terms of buttons and [onSetListenerCallbacks](https://stackoverflow.com/questions/54587517/button-views-in-android/54587592#54587592) – sansa Feb 09 '19 at 12:28
  • @Erik lol, from Android docs --> the lifecycle **callback** methods [link](https://developer.android.com/guide/components/activities/activity-lifecycle) – ardiien Feb 09 '19 at 13:51

2 Answers2

2

Chris, Imagine that you have a function:

fun test() {
  ...
}

Then you decided to add some magic to it. For instance, add 'block' that could be done after function test finished its job. So, here we need to put some extra into code:

interface CallbackInterface {
  fun doJob()
}

and your function become:

fun test(block: CallbackInterface) {
  ...
  block.doJob()
}

so then you can call your test function like this (or pass CallbackInterface into test function):

test(object: CallbackInterface {
  override fun doJob() {
    ...
  }
})

In general, the point is to pass the interface as a parameter in function and call it whenever you want and do on another end do whatever you want with the results.

or in Kotlin you can do like this:

fun test(block: ()-> Unit) {
  ...
  block.invoke() // or just block()
}

and use it:

test {
  ...
}
ardiien
  • 767
  • 6
  • 26
1

Here is an answer from geeksforgeeks

Quoting text:

// Java program to illustrate synchronous callback

interface OnGeekEventListener { 

    // this can be any type of method 
    void onGeekEvent(); 
} 

class B {

private OnGeekEventListener mListener; // listener field 

// setting the listener 
public void registerOnGeekEventListener(OnGeekEventListener mListener) 
{ 
    this.mListener = mListener; 
} 

// my synchronous task 
public void doGeekStuff() 
{ 

    // perform any operation 
    System.out.println("Performing callback before synchronous Task"); 

    // check if listener is registered. 
    if (this.mListener != null) { 

        // invoke the callback method of class A 
        mListener.onGeekEvent(); 
    } 
} 

// Driver Function 
public static void main(String[] args) 
{ 
    B obj = new B(); 
    OnGeekEventListener mListener = new A(); 
    obj.registerOnGeekEventListener(mListener); 
    obj.doGeekStuff(); 
} 

}

class A implements OnGeekEventListener { 

    @Override
    public void onGeekEvent() 
    { 
        System.out.println("Performing callback after synchronous Task"); 
        // perform some routine operation 
    } 
    // some class A methods 
} 

Output:

Performing callback before synchronous Task Performing callback after synchronous Task Asynchronous Callback

An Asynchronous call do not block the program from the code execution. when the call returns from the event the call returns back to the callback function. So in the context of java we have to Create a new thread invoke the callback method inside that thread. Callback may be invoked from a thread but is not a requirement. A Callback may also start a new thread thus making themselves asynchronous.

Below is the simple implementation of this principal.

filter_none edit play_arrow

brightness_4 // Java program to illustrate Asynchronous callback

interface OnGeekEventListener { 

    // this can be any type of method 
    void onGeekEvent(); 
} 

class B {

private OnGeekEventListener mListener; // listener field 

// setting the listener 
public void registerOnGeekEventListener(OnGeekEventListener mListener) 
{ 
    this.mListener = mListener; 
} 

// My Asynchronous task 
public void doGeekStuff() 
{ 

    // An Async task always executes in new thread 
    new Thread(new Runnable() { 
        public void run() 
        { 

            // perform any operation 
            System.out.println("Performing operation in Asynchronous Task"); 

            // check if listener is registered. 
            if (mListener != null) { 

                // invoke the callback method of class A 
                mListener.onGeekEvent(); 
            } 
        } 
    }).start(); 
} 

// Driver Program 
public static void main(String[] args) 
{ 

    B obj = new B(); 
    OnGeekEventListener mListener = new A(); 
    obj.registerOnGeekEventListener(mListener); 
    obj.doGeekStuff(); 
} 

}

class A implements OnGeekEventListener { 

    @Override
    public void onGeekEvent() 
    { 
        System.out.println("Performing callback after Asynchronous Task"); 
        // perform some routine operation 
    } 
    // some class A methods 
} 

Output:

Performing operation in Asynchronous Task Performing callback after Asynchronous Task

Erik
  • 5,039
  • 10
  • 63
  • 119