I wrote code using AsyncTask, which, when finished in onPostExecute, should change the static variable of the main class. AsyncTask starts at the touch of a button, and changes the variable only after it has been fully processed, and not during the time. As a result, you have to press twice to change the value of a variable. How can this be fixed?


The method in which the variable should change (is in a different class):

@Override // protected void onPostExecute(String s) { super.onPostExecute(s); String resultPOST = "Сервер ничего не выдал"; resultPOST = s; MainActivity.serverAnswer+=resultPOST; } 

Main class:

 public class MainActivity extends AppCompatActivity { static Button logInButton; static String serverAnswer = "Ответ от сервера:\n"; String pass,login; public static TextView serverAnswerTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); logInButton = (Button) findViewById(R.id.button); serverAnswerTextView =(TextView) findViewById(R.id.textView); } public void onClickLogIn(View v){ TextView mLogIn = (TextView) findViewById(R.id.editText); login = mLogIn.getText().toString(); TextView mPass = (TextView) findViewById(R.id.editText2); pass = mPass.getText().toString(); new LocationSender().execute(login); serverAnswerTextView.setText(serverAnswer); } 

Forgot to add. If in onPostExecute I change the value of the TextView directly (not through a variable), it changes immediately, without pressing again.

  @Override protected void onPostExecute(String s) { String resultPOST = "Сервер ничего не выдал"; resultPOST = s; MainActivity.serverAnswer+=resultPOST; //текст метки меняется сразу же MainActivity.serverAnswerTextView.setText (MainActivity.serverAnswerTextView.getText()+resultPOST); } 
  • You do not understand how AsyncTask works. I recommend to watch video tutorials on asyncaster before trying to use them - Vladyslav Matviienko
  • I really work with him for the first time. Do you have anything in mind that could help me in this? - Jorik
  • Google can help google.com.ua/… - Vladyslav Matviienko

1 answer 1

Completion of the AsyncTask.execute(login) method does not mean that by this time the thread has completed its work. This method only starts the stream, it does not wait for its completion, therefore at the time of calling the serverAnswerTextView.setText(serverAnswer); method serverAnswerTextView.setText(serverAnswer); after running AsyncTask'a , serverAnswer still nothing in the AsyncTask'a variable.

The correct solution is to remove the text installation after launching the asink tax, and leave it in the onPostExecute method. It is for this purpose created to process the result of the task.

Read more about AsyncTask in the official documentation .

  • @Jorik something I did not quite understand your comment. The solution here is one - to change in onPostExecute - temq
  • I apologize, I did not immediately understand your answer. - Jorik