Reputation: 696
I am having problems trying to get to onPostExecute to fire on this piece of code, I am new to java and especially multithreading so if what i'm doing is completely wrong please feel free to correct me...need to learn.
So the problem is with this:
public class DataTask extends AsyncTask<Void, Void, Integer> {
Context context;
DataTask(Context context) {
this.context = context.getApplicationContext();
}
// Global Int for counting how many Tasks have been completed
int asynCount = 0;
ArrayList<String> arr_dataVts=new ArrayList<String>();
ArrayList<String> arr_dataNtm=new ArrayList<String>();
ArrayList<String> arr_dataOdas=new ArrayList<String>();
ArrayList<String> arr_dataMetAll=new ArrayList<String>();
ArrayList<String> arr_dataMet3HrTask=new ArrayList<String>();
ArrayList<String> arr_dataTideTask=new ArrayList<String>();
@Override
protected Integer doInBackground(Void... params) {
//VtsAsyncTask
VtsTask task1 = new VtsTask();
task1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
//NtmAsyncTask
NtmTask task2 = new NtmTask();
task2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
//OdasAsyncTask
OdasTask task3 = new OdasTask();
task3.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
//MetAllTask
MetAllTask task4 = new MetAllTask();
task4.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
//Met3HrTask
Met3HrTask task5 = new Met3HrTask();
task5.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
//TideTask
TideTask task6 = new TideTask();
task6.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return 1;
}
All the tasks execute as should do
but here in the onPostExecute:
@Override
protected void onPostExecute(Integer result) {
if (asynCount == 6){
//start intents for main activity
System.out.println("asynCount has reached= " + asynCount + " so now starting MainActivity");
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putStringArrayListExtra("data1", arr_dataVts);
intent.putStringArrayListExtra("data2", arr_dataNtm);
intent.putStringArrayListExtra("data3", arr_dataOdas);
intent.putStringArrayListExtra("data4", arr_dataMetAll);
intent.putStringArrayListExtra("data5", arr_dataMet3HrTask);
intent.putStringArrayListExtra("data6", arr_dataTideTask);
// context.startActivity(intent);
}else{
//update dialogue
}
}
}
The onPostExecute never gets called? I don't know why. I have tried using Integers, Void and Booleans to get it to return but doesn't work. All advice is appreciated. EDIT: I now realise that I should really be using Bundle here for all the extras.
Upvotes: 0
Views: 828
Reputation: 46856
Your DataTask
class is unnecessary. The only thing that you are doing in the background is starting several other AsyncTask's you don't need to wrap all of those execute() commands inside of the own AsyncTask because they already get executed in a background thread just by the very nature of AsyncTask.
Each of these other Tasks that you have needs to have it's own onPostExecute()
that handles the results from that specific task and does something with it.
EDIT:
Here is some pseudocode of how it might look if you combined all of your operations into a single AsyncTask:
public class DataTask extends AsyncTask<Void, Void, Integer> {
Context context;
DataTask(Context context) {
this.context = context; // <-- you already have a context, you don't need to call getApplicationContext();
}
@Override
protected Integer doInBackground(Void... params) {
doSomeWork();
doSomeOtherWork();
doYetSomeMoreWork();
//...
}
@Override
protected void onPostExecute(Integer result) {
doSomethingWithThe(result);
}
}
The doSomeWork()
etc... methods should not be AsyncTasks, just normal methods so that they execute sequentially. That way, when all of them are complete onPostExecute()
will be called.
Upvotes: 2