I am developing an application where I need to update some information every time a user logs in, I also use the database on the phone. For all these operations (updating, extracting data from db, etc.) I use async tasks. Since until now I did not understand why I should not use them, but recently I have experienced that if I perform some operations, some of my async tasks just stop at the preliminary execution and do not switch to doInBackground. It was too weird to leave it like that, so I developed another simple application to check what happened. And, strangely enough, I get the same behavior when the number of asynchronous tasks reaches 5, 6th - with preliminary execution.
Does android have asyncTasks limitations in Activity / App? Or is it just a mistake and needs to be reported? Has anyone experienced the same problem and possibly found a workaround?
Here is the code:
Just create 5 of these threads to run in the background:
private class LongAsync extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { Log.d("TestBug","onPreExecute"); isRunning = true; } @Override protected String doInBackground(String... params) { Log.d("TestBug","doInBackground"); while (isRunning) { } return null; } @Override protected void onPostExecute(String result) { Log.d("TestBug","onPostExecute"); } }
And then create this thread. It will go into preExecute and hang (it will not go to doInBackground).
private class TestBug extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { Log.d("TestBug","onPreExecute"); waiting = new ProgressDialog(TestActivity.this); waiting.setMessage("Loading data"); waiting.setIndeterminate(true); waiting.setCancelable(true); waiting.show(); } @Override protected String doInBackground(String... params) { Log.d("TestBug","doInBackground"); return null; } @Override protected void onPostExecute(String result) { waiting.cancel(); Log.d("TestBug","onPostExecute"); } }
android multithreading android-asynctask
Mindaugas Svirskas Mar 11 2018-12-12T00: 00Z
source share