Maveňツ
Maveňツ

Reputation: 1

AsyncTask (Asynchronous processes)

I have a problem regarding Async task

Async from android

Using 2 activities "A" and "B"

by entering a word to search from the url and and store value in DTO and then fetching values from getter and setter.

My complication is that i have implemented async in my activity "B" and that activity is fetching value from same DTO.

Problem is that how my post know that do in background have fetched value from DTO and DTO have fetched value from internet...in case of slow internet connection.

I m sending intent from "A" to "B" and showing the results on "B"

PROBLEM:

1. If i remove async then app shows black page and also freezes (in case of slow connection only) but data is displayed

2. If i use aync then sometimes progress dialog show for long time and inspite of knowing that data is already displayed in UI

code links https://www.dropbox.com/s/p27rpokz68sryv3/SearchData.java

https://www.dropbox.com/s/rm3i52djiay327u/SearchData_DTO.java

https://www.dropbox.com/s/2hpufx2a12480on/Search.java

Pls suggest me the possible solution for this

Regards

Upvotes: 0

Views: 750

Answers (1)

Pramod
Pramod

Reputation: 1133

You need to listen for asyntask complete listener, For that let your activity A impliment interface and call that method from Activity B,s Asyntask,s onpostexecute method Thus your activity A will come to know that B has finished his task and you can do next thing.. Hope this helps

    public interface AsyncTaskCompletedListener {
        public void OnResultSucceeded(String result);
    }

    public class LoginAsyncTask extends AsyncTask<String, Void, String> {
        AsyncTaskCompletedListener mAsyncTaskCompletedListener;

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            mAsyncTaskCompletedListener.OnResultSucceeded(result);
        }
    }

Here is the interface Let Activity A impliment this and from Activity invoke this from onpostexecute

From Activity A

LoginAsyncTask customloginasync = new LoginAsyncTask(getActivity(),
                FATCH_USER_LIST, arglist);

        customloginasync.execute();

        customloginasync.setOnResultsListener(new AsyncTaskCompletedListener() {

            @Override
            public void OnResultSucceeded(String result, int asyncTaskNo) {

                Logger.logInfo("CustomLogin data=========" + result);

                ParseAvailableUserData(result);

            }
        });

Upvotes: 1

Related Questions