I'm having an issue trying to prevent an intent to Activity from happening until an API call has been consumed and processed. I have a button that when clicked will call two methods. The first method 'gatherResponceAndProcess' gets a response, imports the data, and converts it into some new objects. This part is working. However, moveToNextActivity gets called before gatherResponceAndProcess has finished. I've tried all sorts of things, but to no avail. I'm at a loss where or what needs to be done. Please help.
public class MainActivity extends AppCompatActivity {...
...
Button.setOnClickListener(v -> {
gatherResponceAndProcess(); // <<<<<<< CALLS API AND IMPORTS DATA
moveToNextActivity();
});
private void gatherResponceAndProcess() {
try {
Helper helper = new Helper(buildURL());
helper.fetchAndProcess();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
}
}
private void moveToNextActivity() {
Intent intent = new Intent(mContext, NextActivity.class);
Log.d(TAG, "moveToNextActivity: Creating Intent");
mContext.startActivity(intent);
}
...
public class Helper implements APICall.AsyncResponse {...
...
public void fetchUsersObservations() {
APICall call = new APICall(URL);
call.execute(); //Calls processFinish() when finsished
}
@Override
public void processFinish(JSONArray results) {//THIS HAS TO FINISH BEFORE moveToNextActivity() SHOULD BE CALLED
//map json to classes
arrayOfClasses(oldClasses)
//massage the data
arrayOfNewClasses.add(massagedData)
MyNewClass.importMassagedData(arrayOfNewClasses); //NextActivity Needs this
}
...
public class APICall extends AsyncTask {...
...
URL APIUrl;
int page = 1;
private Helper helper = Helper();
public INatCall(URL url) {
this.iNatUrl = url;
}
@Override
protected Object doInBackground(Object[] objects) {
return restCall(APIUrl);//Returns JSONObject
}
@Override
protected void onPostExecute(Object o) {
helper.processFinish((JSONArray) o)
}
What I ended up doing was calling moveToNextActivity(); from Helper.processFinish. It's not Ideal because I have to pass Context around.
public class MainActivity extends AppCompatActivity {...
...
Button.setOnClickListener(v -> {
gatherResponceAndProcess(); // <<<<<<< CALLS API AND IMPORTS DATA
//moveToNextActivity(); <--Removed
});
private void gatherResponceAndProcess() {
try {
Helper helper = new Helper(buildURL());
helper.fetchAndProcess(mContext);
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
}
}
private void moveToNextActivity(Context context) {
Intent intent = new Intent(context, NextActivity.class);
Log.d(TAG, "moveToNextActivity: Creating Intent");
context.startActivity(intent);
}
...
public class Helper implements APICall.AsyncResponse {...
...
public void fetchUsersObservations(Context context) {
APICall call = new APICall(URL);
call.execute(); //Calls processFinish() when finsished
}
@Override
public void processFinish(JSONArray results) {//THIS HAS TO FINISH BEFORE moveToNextActivity() SHOULD BE CALLED
//map json to classes
arrayOfClasses(oldClasses)
//massage the data
arrayOfNewClasses.add(massagedData)
MyNewClass.importMassagedData(arrayOfNewClasses); //NextActivity Needs this
moveToNextActivity(mContext); //<-- Added Here
}
...