Issue
I have an AsyncTask in my project and there is an alert which says:
This AsyncTask class should be static or leaks might occur (anonymous android.os.AsyncTask) less... (Ctrl+F1) A static field will leak contexts. Non-static inner classes have an implicit reference to their outer class. If that outer class is for example a Fragment or Activity, then this reference means that the long-running handler/loader/task will hold a reference to the activity which prevents it from getting garbage collected. Similarly, direct field references to activities and fragments from these longer running instances can cause leaks. ViewModel classes should never point to Views or non-application Contexts.
here is my code that is contain this alert :
ProgressDialog progressDialog;
AsyncTask<String,Void,Boolean> asyncTask = new AsyncTask<String, Void, Boolean>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setTitle("بارگذاری");
progressDialog.setMessage("در حال دریافت اطلاعات از پایگاه داده..");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected Boolean doInBackground(String... strings) {
Cursor cursor = DataBase.getinfos(page,limit);
if (cursor.isAfterLast()){
return false;
}else {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setId(cursor.getInt(0));
propertyInfo.setAddress(cursor.getString(1));
propertyInfo.setDetails(cursor.getString(2));
propertyInfo.setOptions(cursor.getString(3));
propertyInfo.setMortgage_cost(cursor.getLong(4));
propertyInfo.setRent_cost(cursor.getLong(5));
propertyInfo.setOwner_name(cursor.getString(6));
propertyInfo.setUnits_per_floor(cursor.getInt(7));
propertyInfo.setCurrent_floor(cursor.getInt(8));
propertyInfo.setFloors_count(cursor.getInt(9));
propertyInfo.setRoom_count(cursor.getString(10));
propertyInfo.setOwner_phone(cursor.getString(11));
propertyInfo.setDocument_type(cursor.getString(12));
propertyInfo.setRequest_type(cursor.getString(13));
propertyInfo.setProperty_type(cursor.getString(14));
propertyInfo.setCost(cursor.getLong(15));
propertyInfo.setArea(cursor.getInt(16));
propertyInfo.setHouse_type(cursor.getString(17));
propertyInfo.setLocation(cursor.getString(19));
propertyInfo.setNoeMorajeKonande(cursor.getString(18));
propertyInfo.setShomareSafhe(cursor.getString(20));
propertyInfo.setDate(cursor.getString(21));
arrayList.add(propertyInfo);
lastRecivedDataSize++;
}
return true;
}
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
loading = aBoolean;
if (arrayList.isEmpty()) {
setContentView(R.layout.no_result);
} else {
mAdapter = new RecyclerInfoAdapter(arrayList, ShowAllDataActivity.this);
mAdapter.notifyDataSetChanged();
recyclerView.setAdapter(mAdapter);
recyclerView.scrollToPosition(pastVisiblesItems + visibleItemCount - 1);
page++;
}
progressDialog.dismiss();
}
};
asyncTask.execute();
anybody knows the problem ??
Solution
Create a separate class that implements AsyncTask, then in your activity instantiate it, and run the execute method.
Answered By - ynsmtkl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.