I've read a lot of questions on AsyncTask problems of different kinds, but I seem not to find the problem I'm facing.
Android Studio insists on making my custom AsyncTask static with the following note:
This AsyncTask class should be static or leaks might occur
For that reason I've been trying on using WeakReference to the Activity the task runs in. Problem is that I want to use findViewById() in onPre and onPostExecute and I cannot to access the method. I've found that:
weakReference.get().findViewById(R.id.dictionaryLoading).setVisibility(View.VISIBLE);
does not produce an error in Android Studio, but it crashes the app.
How to solve the problem of not accessing findViewById() in the custom static AsyncTask?
Code generally works if I get rid of weakReference and make AsyncTask it non-static
Here's my AsyncTask: (the empty while loop is there on purpose so it waits until the global variable has some data.
public static class LoadingDictionaryAsync extends AsyncTask<Void, Void, Void> {
private final WeakReference<MainScreenActivity> weakReference;
private LoadingDictionaryAsync(MainScreenActivity mainScreenActivity){
this.weakReference = new WeakReference<>(mainScreenActivity);
}
@Override
protected void onPreExecute() {
weakReference.get().findViewById(R.id.dictionaryLoading).setVisibility(View.VISIBLE);
weakReference.get().findViewById(R.id.dictionaryListView).setVisibility(View.GONE);
}
@Override
protected Void doInBackground(Void... voids) {
if (vocabulariesGlobal == null || vocabulariesGlobal.size() == 0) {
weakReference.get().loadDatabaseVocabularyToDictionary();
}
while (vocabulariesGlobal == null || vocabulariesGlobal.size() == 0) {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
weakReference.get().loadDictionaryOnTheList(vocabulariesGlobal);
weakReference.get().findViewById(R.id.dictionaryLoading).setVisibility(View.GONE);
weakReference.get().findViewById(R.id.dictionaryListView).setVisibility(View.VISIBLE);
}
}
And that's how I am calling it:
LoadingDictionaryAsync loadingDictionaryAsync = new LoadingDictionaryAsync((MainScreenActivity)getParent());
loadingDictionaryAsync.execute();
EDIT:
I've tried with creating a separate file with the LoadingDictionaryAsync where static is not a problem anymore, but I still have problem with passing MainScreenActivity it, so findViewById() cannot be used.
What is the proper way of passing Activity? I want to pass it in onNavigationItemSelected() and onQueryTextChange() in SearchView?