Wait to call Intent while Processing data from API

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)
}


from Recent Questions - Stack Overflow https://ift.tt/3o3NjFC
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)