Asked  1 Year ago    Answers:  5   Viewed   184 times

I want to understand the concept of callback. I have searched on internet about the callbacks and there are many examples using interface, and one class is calling a method of another class using that interface. But still I can't get the main concept of callbacks, what is the purpose of using callbacks?



Here is a nice tutorial, which describes callbacks and the use-case well.

The concept of callbacks is to inform a class synchronous / asynchronous if some work in another class is done. Some call it the Hollywood principle: "Don't call us we call you".

Here's a example:

class A implements ICallback {
     MyObject o;
     B b = new B(this, someParameter);

     public void callback(MyObject o){
           this.o = o;

class B {
     ICallback ic;
     B(ICallback ic, someParameter){
         this.ic = ic;

    new Thread(new Runnable(){
         public void run(){
             // some calculation

interface ICallback{
    public void callback(MyObject o);

Class A calls Class B to get some work done in a Thread. If the Thread finished the work, it will inform Class A over the callback and provide the results. So there is no need for polling or something. You will get the results as soon as they are available.

In Android Callbacks are used f.e. between Activities and Fragments. Because Fragments should be modular you can define a callback in the Fragment to call methods in the Activity.

Wednesday, June 9, 2021

SharedUserId is used to share the data,processes etc between two or more applications. It is defined in AndroidManifest.xml like,


and define the shared parameter in for that app, like


Hope its helpful to you.

Wednesday, June 2, 2021

R.color.whatever is an int. It's automatically generated as a reference to an externally defined (in XML) resource. When you call setBackgroundColor with this integer, it's trying to parse this int's value as a Color. setBackgroundResource expects to get a resource integer passed to it. It retrieves the externally defined value, and then applies the color that way. As for setBackgroundColor, try using a full hex value color with alpha included, e.g. 0xFFFFFFFF (where the first two F values are the alpha value).

EDIT: Beaten by Mark! :P

Thursday, August 19, 2021

I think you have 2 options:

The fake progress bar approach

You know in advance how many files you need to download, you can set the ProgressDialog total amount to the number of files to download. This works pretty well with files which are small and similar in dimensions and gives the user a good feedback about what's going on.

// you can modify the max value of a ProgressDialog, we modify it
// to prevent unnecessary rounding math.
// In the configuration set the max value of the ProgressDialog to an int with

for (int i = 0; i < urls.length; i++) {
    // launch HTTP request and save the file
    // your code 

    //advance one step each completed download

 * Updating progress bar
protected void onProgressUpdate(Integer... progress) {

The real progress bar approach

You need to know in advance the total length of all the files you need to download. For example, you could create a separate REST API to call before everything else which give you the total length in bytes before you start to download each separate files. In this way, you can periodically update the total ProgressDialog length accordingly to the total bytes you have already downloaded.

Tuesday, August 31, 2021

do it with a receiver. you can be notified about network state change. for example,

private BroadcastReceiver networkReceiver = new BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
     super.onReceive(context, intent);
     if(intent.getExtras()!=null) {
        NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
        if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
            // we're connected
     // we're not connected 

register this in your onResume(), and unregister on onPause().

protected void onPause() {

protected void onResume() {
    IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(networkReceiver, filter);

additionally, to obtain the initial state before your receiver has been registered, call this in your onResume() method,

public boolean isNetworkConnected() {
        ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        return false;

make sure your app requests this permission,

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Friday, September 10, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :