I learned about callback when I started learning fragments in android which is use to communicate between fragments and activity. Though Callback originally came back in the days of c/c++, we can do the same thing in Java using interface.

What is a callback method?

A callback method is a way to notify or inform a class that a task in another class is done. In plain english example, a conversation between two guy would be like this

    Hey man, wake me up when september ends*.
    Sure thing bro!
  ................... Task being done here.................
Dude2 to Dude1
    Wake up, September has ended.

Well, that’s how I imagine it. Anyway lets see some code example first then see what is going on in the code.

How to create a Callback Method?

To have a callback method, we need to use an interface

public interface IOnClickEvent {
    void onClick();

class ClickListener {

    IOnClickEvent iOnClickEvent;

    ClickListener(IOnClickEvent iOnClickEvent) {
        this.iOnClickEvent = iOnClickEvent;

     private void CallClick(){
        System.out.println("Doing Some work here ...........");
public class Button implements IOnClickEvent {

    private ClickListener clickListener;

    void CallEvent(){
        System.out.println("Initial Call");
        clickListener = new ClickListener(this);   // polymorphism in action right here!

    public void onClick() {
        System.out.println("Called Back");
public class Main {
    public static void main(String[] args) {

        Button b = new Button();


Initial Call
Doing Some work here ...........
Called Back

The fun stuff

When we call the CallEvent() what is happening is that we instantiate the clickListener variable by passing the this keyword. This is polymorphism in action by the way. Since the Button class implements the IOnClickEvent interface, we can pass “this”. When the constructor of ClickListener was called it pass its parameter’s reference to its instance variable the iOnClickEvent which has the data type of IOnClickEvent(our interface). It then calls the CallClick() which in turn calls the


and guess from who’s onClick() is that? It’s from Button class . How? Remember this code?

clickListener = new ClickListener(this);

we are passing the button itself. We can think of t his as doing button.onClick();.

I know, confusiiiiiiiiing! Here might help to visualize


Android Example

I said from the start that I learned callbacks because I was using fragments. Well here is an example

public class SampleFragment extends Fragment {

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onCheck(boolean mark);
    private OnFragmentInteractionListener mListener;
    boolean mark;
    Button button;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_sample, container, false);

        button = view.findViewById(R.id.click);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mark = !mark;
        return view;
    public void onAttach(Context context) {
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;   // this is where we attach the main activity to our mListener
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
    public void onDetach() {
        mListener = null;

// We implement the Interface
public class MainActivity extends AppCompatActivity implements SampleFragment.OnFragmentInteractionListener { 

    CheckBox checkBox;
    protected void onCreate(Bundle savedInstanceState) {
        checkBox = findViewById(R.id.chc);

    public void onCheck(boolean mark) {

Alt Text


Callbacks is kinda hard to understand and it’s a bit confusing. It takes time to get used to this pattern since it needs you to change the way you think so don’t stress yoursefl too much. That’s it for today. Remember to Smile, Code and Wave!