Friday, 30 October 2015

How to get call back from AsyncTask or any other class


I am creating AsyncTask(Sample) for getting some data from server after that get callback to our activity. For this purpose I am writing interface and LoaderManager class.

MainActivity.java

package demo.com.applicationcallback;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnGetCallBack:
                getCallBackFromLoaderManager();
                break;
        }
    }

    private void getCallBackFromLoaderManager() {
        /*if you want to pass some data in LoaderManager
         class you can pass in Constructor*/
        LoaderManager loaderManager = new LoaderManager(new CustomCallback() {

            @Override
            public void success(String message) {
               /* if successfully get data from server, control gets back here
                you can write code here as per the your requirement*/
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void fail(String message) {
                /*fail due to some reason control gets back here according to your requirement write your code*/
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });
        loaderManager.execute(/*here we can pass some data as per the our requirement*/);
    }
}

LoaderManager.java

package demo.com.applicationcallback;

import android.os.AsyncTask;

public class LoaderManager extends  AsyncTask<String, Void, Boolean> {

    private CustomCallback mCustomCallback;

    public LoaderManager(CustomCallback customCallback) {
        this.mCustomCallback= customCallback;
    }


    @Override
    protected Boolean doInBackground(String... params) {
        // write  code for getting data from server
        return true;
    }

    @Override
    protected void onPreExecute() {
        //write code show progress bar or some custom loading indicator etc...
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {

        if(aBoolean){
            mCustomCallback.success("Get data Successfully");
        }else {
            mCustomCallback.fail("Failure due to some reason");
        }
    }
}

CustomCallback.java

package demo.com.applicationcallback;

public interface CustomCallback {
    public void success(String message);
    public void fail(String message);
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:orientation="vertical"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnGetCallBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="initiateCallback" />

    <TextView
        android:id="@+id/tvShowMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>









No comments:

Post a Comment