Full width home advertisement

Watch Tutorials

Post Page Advertisement [Top]

I will teach you how you can create your own callback in java using real scenario explaining it step by step. Callbacks are attained using pointers, as java does not support pointers callbacks can still be attained in it.

This tutorial is also covered in detail in this video:


What is a callback?

A callback is attained by passing one function pointer to the second function. In the world of Java, it is a method which is called by one class and executed in the second class. 

Commonly it is used in asynchronous programming for example, handling the response of the server API (Application Programming Interface) call.

Enough of theory let's dive into the code & create the one ourselves! 

So, I have created a very simple application in which drinks are suggested to the user. When the user opens the application an API call is made to get the drink's name, meanwhile feedback is shown to the user in the form of the progress bar.

There are two classes one is Main Activity and the other is Drinks API. In Drinks API, drinks name is retrieved through an API call. To mimic a server request I have created an async task and added a sleep in the thread for 2 seconds. So, in this way there is a little bit of time delay and in the meanwhile progress bar is shown to the user. After the processing is done, in our case its 2 seconds delay, then the progress bar will go away.

Problem

Let's run the application at this stage and see what happens. 

No drink is shown on the screen it is because our API call is taking time to execute, and method has returned an empty string that is initialized at the time of creation of the object of this class. 

The problem here is that our Main Activity doesn't know when the drink's name is returned from the Drinks API. Some sort of response back from the Drinks API is required here so that it can display that to the user.

Solution

The solution for this problem is to have a callback which sends response back to the Main Activity so that it can display the information to the user. Callback is attained by passing one function pointer to the function as java does not support the pointers so we can implement this by making an interface. 

Create a new interface, let's name it as Drinks API call back. You can create it separately or within the Drinks API class, I prefer creating it within the Drinks API class. So here is our callback, it should have a method which will pass the name back to the main API.

Let's create a method, name it as “displayDrinks”. Now we need to create a reference for this callback in this Drinks API, name it as API callback or whatever you like, callback reference is created in the Drinks API. So now we need to call this “displayDrink” method when the API is returning the drinks name. 


Call the “displayDrink” method from the API callback reference and pass the drink name in it. In order to pass the callback implementation to the API, we need to create a setter for it. Now we are ready to use this callback to pass the drinks name back to the main activity. 
Now we need to implement this callback in the Main Activity, type implements after the parent class AppCompatActivity and then our callback name which is Drinks API callback.
Android Studio should an error and the reason for that is the displayDrink method of the callback must be implemented. Click on the error and then implement its method displayDrink. 

Click ok, android studio will implement this method for us, now there should be a new method displayDrink  in the main activity.


We can now use drink name from the method param, set the drink name to the text view and visibility of the progress bar to set it to invisible. Our method is ready here so it will pass the drinks name and then we can set this drink to the text view. 


The last thing left here is to pass this implementation to the Drinks API using the setter created earlier. Call the setter from the DrinksApi reference and pass “this” as an argument for this class.


We have used "this" keyword as a parameter for the setter which means referring to the current object. Which is in our case is the MainActivity class.

All set now let us run the application again. As we can see progress bar is shown to the user for two seconds and then drink name is displayed, our callback is working as expected.


 

No comments:

Post a Comment

Share your thoughts ...

Bottom Ad [Post Page]