I'm doing a list with the download from AsyncTask.
All is well, only when i'm exit from the application and re-launch it, list increases by 2 times = AsyncTask works twice when i'm start Activity. Can I somehow run it 1 time and generally make it static so that it is not attached to Activity?
public class SpecialtyListActivity extends AppCompatActivity {
private static final String URL = "URL..";
public static ArrayList<Specialty> specialtyList = new ArrayList<>();
public static ArrayList<Worker> workerList = new ArrayList<>();
ListView listView;
EmployeesListFragment employeesListFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.specialty_list);
listView = findViewById(R.id.spec_list);
employeesListFragment = new EmployeesListFragment();
if (Utils.isNetworkAvailable(this)) {
new DataLoader(this).execute(URL);
} else {
Toast.makeText(this, "No Network Connection", Toast.LENGTH_LONG).show();
}
}
@SuppressLint("StaticFieldLeak")
class DataLoader extends AsyncTask<String, Void, Void> {
@SuppressLint("StaticFieldLeak")
private Context mContext;
private ProgressDialog pdialog;
private final String TAG = getClass().getSimpleName();
DataLoader(Context context) {
mContext = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pdialog = ProgressDialog.show(mContext, "__", "___");
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (mContext != null) {
SpecialtyListAdapter adapter = new SpecialtyListAdapter(this.mContext, R.layout.specialty_list, specialtyList);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
pdialog.dismiss();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
@Override
protected Void doInBackground(String... params) {
try {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(params[0]);
try {
JSONObject reader = new JSONObject(jsonStr);
JSONArray response = reader.getJSONArray("response");
for (int i = 0; i < response.length(); i++) {
String workerFName = response.getJSONObject(i).getString("f_name");
String workerLName = response.getJSONObject(i).getString("l_name");
String workerBithday = response.getJSONObject(i).getString("birthday");
String workerAvatarUrl = response.getJSONObject(i).getString("avatr_url");
int workerSpecialtyId = response.getJSONObject(i)
.getJSONArray("specialty")
.getJSONObject(0)
.getInt("specialty_id");
String specialtyName = response.getJSONObject(i)
.getJSONArray("specialty")
.getJSONObject(0)
.getString("name");
Specialty specialty = new Specialty(workerSpecialtyId, specialtyName);
if (!specialtyList.contains(specialty)) {
specialtyList.add(specialty);
}
Worker worker = new Worker(workerFName, workerLName);
worker.setBithday(workerBithday);
worker.setAvatarLink(workerAvatarUrl);
worker.setSpecialty(workerSpecialtyId);
workerList.add(worker);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
I also read that AsyncTask should be static, how to do that?