Vaia Vaia - 1 year ago 70
Java Question

Best practice using AsyncTask for SharedPreference and general initializing?

First, I have to say I'm kinda new to native Android development. I put the following code inside onCreate in my


I never used AsyncTask, so I don't know if this is the best practice to do it. Should I create a new AsyncTask-class and load it somehow in the onCreate method? I put it in the MainActivity onCreate method, because of the scope of variables like
(billing purchase class), pollfish, counting app-launches, etc.

Also I don't know what to return? Because I have multiple results. There are many tutorials how to create an AsyncTask like this and also suggestions to store SharedPreferences there, but I want to know the best practice for it. For example to get values later in my onResume method or similiar operations.

new AsyncTask<Context, Void, String>()
protected String doInBackground(Context... params)
Context context = params[0];
SharedPreferences pref = context.getSharedPreferences("Pref",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();

// Count App Launches
totalCount = pref.getInt("counter", 0);
editor.putInt("counter", totalCount);

// Pollfish ADS
int pollfishPref = pref.getInt("pollfish", 0);
Log.d(TAG,"Application launch count: " + totalCount);
Log.d(TAG,"pollfish count: " + pollfishPref);

if (!bp.isPurchased(PRODUCT_ID) && (pollfishPref == 0 || (pollfishPref != 0 && pollfishPref <= totalCount)))
PollFish.ParamsBuilder paramsBuilder = new PollFish.ParamsBuilder("xxx")
PollFish.initWith(MainActivity.this, paramsBuilder);
mTracker.setScreenName("Pollfish Count: " + pollfishPref + " Applaunch Count: " + totalCount);
mTracker.send(new HitBuilders.ScreenViewBuilder().build());

Boolean consumed = pref.getBoolean("consumed", Boolean.parseBoolean(null));
Log.d(TAG,"consumedPref: " + consumed);

if (!consumed)
editor.putBoolean("consumed", true);
Log.d(TAG,"consumed now!");

return xxx;

protected void onPostExecute(String result)
Log.d(TAG, "Preference received in background: " + result);

Answer Source

Generally, the best practice is for re-use of the AsyncTask and cleaner code by having a separate class. If you are concerned about variable scope, you can pass those into a constructor for your AsyncTask.

class MyAsyncTask extends AsyncTask<Params, Progress, Result> {
    private Object arg1;

    public MyAsyncTask(Object arg1) {
        this.arg1 = arg1;

    public Result doInBackground(Params... params) { }

    public void onPostExecute(Result result) { }

Of course, update the necessary object types, and use it like so

new MyAsyncTask(arg1).execute();

You may want to update and retrieve values from the SharedPreferences outside of the AsyncTask then pass in those respective values to the constructor as shown.

I don't know what to return? Because I have multiple results.

Sadly, you can only return one type of class. If you are concerned with how to get a result back from the AsyncTask back to where you called it from, then I would recommend callbacks.