Using variables within the onPostExecute method

Asked

Viewed 42 times

1

I am using Asynctask in my project and ended up generating a doubt in the passage of variables, in the method doInBackground i return a cursor variable that is passed by Return. I wanted to know what is required to use Return, because if I pass Return as null I can still use my variable in the method onPostExecute. I can do the same example below:

public class SearchGeocode extends AsyncTask<String, Integer, Cursor> {
        public Cursor minhaVariavel;
        public Boolean verifica;

        protected Cursor doInBackground(String... countryTitle) {

        minhaVariavel = SqliteDatabase;

        verifica = false;

        return null;
        }

         protected void onPostExecute(Cursor cursort) {
                super.onPostExecute(cursort);

        adapSerchView = new AdapSerchView(context, minhaVariavel, 0);

        if (verifica){
        //Faça Algo
        }

        }

        }

1 answer

1


You are not required to pass as Return, the two forms are valid.

If you had more than one variable to pass (unusual situation, but it happens) you would have to use the form without Return, which is easier to understand than using both forms at the same time. So you can choose from the two ways you like the most.

You omit the need to use the return passing as generic parametric type the type Void in place of Cursor.

  • Another thing in the onPostExecute method I can call again the same Asynctask?

  • It depends. Only if it is another instance, the same instance is only executed once. But think about whether this is what you need, because wanting to run two Asynctasks in sequence may be indicative of having a wrong implementation intention (for example, you could rework this to use only one Asynctask instead).

  • Yes I understand. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.