0
votes

I am currently creating an email application which has a heavy reliance on AsyncTask.

Within the app, there are five tabs with a fragment in each (Inbox, Outbox, Sent, Saved & Contacts) where all fragments are loaded at the same time from MainActivity. As well as this, there is a background thread for handling the incoming mail as well as a compose activity and view activity (both of which also use AsyncTask to load the components).

As each tab / fragment is opening a database connection, a loading spinner has been incorporated. This spinner is started in onPreExecute but will remain spinning until onPostExecute is called. However, on some occasions, the onPostExecute isn't called and neither is the doInBackground.

After doing some research, I believe the doInBackground likely isn't executed due to the fact that the thread limit / number of simultaneous AsyncTask functions have been reached.

Therefore, how can I resolve this so that the doInBackground is always executed?

MainActivity - creating some fragments:

private void createInboxFragment() { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); Fragment mailFragment = new InboxFragment(); transaction.add(R.id.layout_main, mailFragment, "Inbox"); transaction.commit(); } private void createOutboxFragment() { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); Fragment mailFragment = new OutboxFragment(); transaction.add(R.id.layout_main, mailFragment, "Outbox"); transaction.hide(mailFragment); transaction.commit(); } private void createSentFragment() { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); Fragment mailFragment = new SentFragment(); transaction.add(R.id.layout_main, mailFragment, "Sent"); transaction.hide(mailFragment); transaction.commit(); }

Inbox Fragment - AsyncTask

private class SetupUI extends AsyncTask<Void, Void, Boolean> { @Override protected void onPreExecute() { Log.d(TAG, "onPreExecute"); progressOverlay.setVisibility(View.VISIBLE); } @Override protected Boolean doInBackground(Void... params) { Log.d(TAG, "doInBackground"); try { // Start: SetupUI setSharedObjects(); setupLists(); getUnreadCount(); setupNotification(); createSharedRunnable(); initDBFunctions(); setupListView(); setupListViewCAB(); setupListViewClick(); // End: SetupUI } catch (Exception ex) { ACRA.getErrorReporter().handleSilentException(ex); ex.printStackTrace(); handleError(); } return true; } @Override protected void onPostExecute(Boolean result) { Log.d(TAG, "0 - onPostExecute"); AndroidUtils.animateView(progressOverlay, View.GONE, 0, 250); Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { Log.d(TAG, "1 - onPostExecute"); finaliseListView(); } }; handler.postDelayed(runnable, 750); } } 
1

1 Answers

2
votes

Async task can run parallel but it depends on thread pool size.Before android 3.0 thread pool size was 1. For to run simultaneously you can call

asynctask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR , null);

for more detail you can visit https://developer.android.com/reference/android/os/AsyncTask.html#execute(Params...)