0

This is my function to register.

If I get the correct feedback from my server it will change the isCreated to true boolean and move to the next Activity.

The problem is that the boolean cannot be edited, and I need to click the Button again to move to the next Activity (the boolean can only be set to true after I click the Button)?

 public void onClick(View view) {

    switch(view.getId()){
        case R.id.register:
        if(!validate())
                Toast.makeText(getBaseContext(), "Enter user information!", Toast.LENGTH_LONG).show();
            // call AsynTask to perform network operation on separate thread
            new HttpAsyncTask().execute(herokuServer);
            if (isCreated)startActivity(new Intent(this, Preference.class));


            break;
    }

private class HttpAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        newAccount = new userAccount();
        newAccount.setPassword(password.getText().toString());
        newAccount.setEmail(email.getText().toString());

        return POST(urls[0],newAccount);
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        if (result.equals("Welcome to Opshun!"))isCreated = true;
        //if (!result.equals("Welcome to Opshun!")) result = "Please use your email to register!";
        Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
    }
}

Is there any way to fix it?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
chrsfang
  • 1
  • 2
  • you can start the activity in `onPostExecute()` if it's an inner class or use an interface http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648 the code continues to run while the task is running, hence **asynchronous** – codeMagic May 01 '15 at 13:21

2 Answers2

2

This is the whole point behind AsyncTask; it moves long running operations from the UI Thread. By doing this, the UI Thread can "continue" executing while the Thread started by the AsyncTask does the heavy duty operation.

One possible solution could be storing a boolean value in SharedPreferences that determines if the AsyncTask already ran.

If what you want is to be notified when the AsyncTask is done, you need to create a delegate using an interface. This answer shows how to do it.

Community
  • 1
  • 1
Emmanuel
  • 13,083
  • 4
  • 39
  • 53
0

Return the data you get in doInBackground() to onPostExecute() and if the data is what you wanted then start the other activity from there using Intent .

Nikhil Verma
  • 1,777
  • 1
  • 20
  • 41