0

There are 3 handler in my fragment all those contain AsyncTask, and the fragment contains an int value, for example 50. Here what I wanted to do is to compare those 3 int value (which I am fetching in those 3 AsyncTask in handler) with the int value defined in fragment. For example if AsyncTask in handler-1 gets 80, AsyncTask in handler-2 gets 10, AsyncTask in handler-3 gets 46, then I want to compare these 3 ints with that fragment int.

I forgot to tell that after comparison, mFragmentValue needed to update with new value from onPostExecute().

My code is big to post here, so here is an example:

class MyFragment{

    int mFragmentValue = 50;;

    void onViewCreated(){
       handler1.post(calling AsyncTask here using runnable); //Here I get 80 in onPostExecute in MyAsyncTask, now I need to compare this 80 with mFragmentValue. These AsyncTasks are sub class of my fragment.
       handler2.post(calling AsyncTask here using runnable); //Here I get 10 in MyAsyncTask, now I need to compare this 10 with mFragmentValue;
       handler3.post(calling AsyncTask here using runnable); //Here I get 46 in MyAsyncTask, now I need to compare this 46 with mFragmentValue;
    }

    static class MyAsyncTask extend AsyncTask{

        void onPostExecute(){
            // getting int here.
            //need to compare fetched int with mFragmentValue;
        }
    }
}
Nishant Bhakta
  • 2,897
  • 3
  • 21
  • 24
  • You can access `mFragmentValue` directly. – Code-Apprentice Jan 10 '18 at 23:55
  • no I can't. `AsyncTask` is static. I just can send the value to `Asynctask` to compare but I need to update `mFragmentValue` from `onPostExecute()` – Nishant Bhakta Jan 11 '18 at 00:01
  • `MyAsyncTask` is not static in the code you posted here. Is it in your actual code? If so is there a reason that you cannot make it non-static? – Code-Apprentice Jan 11 '18 at 00:22
  • you can hold the reference to the fragment by using WeakReference. Then get fragment instance, access to mFragmentValue and update it in onPostExecute – Truong Giang Dam Jan 11 '18 at 04:14
  • @Code-Apprentice, I am sorry about that but it is static. And because I am getting warning for memory leak, I have to make it static.If I make it non-static I get warning. You can check a question about it https://stackoverflow.com/questions/44309241/warning-this-asynctask-class-should-be-static-or-leaks-might-occur – Nishant Bhakta Jan 11 '18 at 07:37
  • p.s. I suggest you give a **complete** code example. You should show the code. Words describing what the code does is often helpful. This should always be **in addition** to code, not to replace the code. – Code-Apprentice Jan 11 '18 at 16:26

1 Answers1

0

If you only need the value of mFragmentValue in MyAsyncTask, then you can pass it in directly:

class MyFragment{

int mFragmentValue = 50;;

    void onViewCreated(){
        AsyncTask task = new MyAsyncTask();
        task.execute(mFragmentValue);
    }

    static class MyAsyncTask extend AsyncTask{
        int mTaskValue;

        void doInBackground(Integer...values) {
            mTaskValue = values[0];
        }

        void onPostExecute(){
            // Now use mTaskValue
        }
    }
}

Note that there is no need to use a Handler nor a Runnable with an AsyncTask. The purpose of a Handler is to facilitate communication between threads, but AsyncTask already takes care of this for you. In addition, doInBackground() already runs on its own thread and onPostExecute() runs on the main thread, so there is no need to create a Runnable.

If you need to change the value of mFragmentValue, then MyAsyncTask must have a reference to the Fragment. You can do this by making MyAsyncTask non-static or add a constructor which accepts a Fragment as a parameter (or some interface which your fragment implements). Either way, you must find another way to address the memory leak warning. One solution here to add a method to MyAsyncTask that you can call from onPause(), onStop(), or onDestroy(). Calling this method tells the AsyncTask that the Fragment reference will is no longer valid. Then the AsyncTask acts accordingly, probably by aborting its work in doInBackground().

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268