Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>this.dialog.dismiss();</code> could be removed from the if statement, and just have the dialog automatically dismiss whenever <code>onPostExecute</code> runs. </p> <p>Edit: my code suggestion:</p> <p>In the class that launches the AsyncTask:</p> <pre><code> ProgressDialog progressDialog = new ProgressDialog(getActivity()); progressDialog.setMessage("Logging in..."); LoginTask loginTask = new LoginTask((Polling) getActivity(), progressDialog); loginTask.execute(); </code></pre> <p>And the AsyncTask itself:</p> <pre><code>public class LoginTask extends AsyncTask&lt;String, Void, Integer&gt; { private ProgressDialog progressDialog; private Polling activity; public LoginTask(Polling activity, ProgressDialog progressDialog) { this.activity = activity; this.progressDialog = progressDialog; } @Override protected void onPreExecute() { progressDialog.show(); } protected Integer doInBackground(String... arg0) { EditText userName = (EditText)activity.findViewById(R.id.emailEditText); EditText passwordEdit = (EditText)activity.findViewById(R.id.passEditText); String email = userName.getText().toString(); String password = passwordEdit.getText().toString(); UserFunctions userFunction = new UserFunctions(); JSONObject json = userFunction.loginUser(email, password); // check for login response try { if (json.getString(KEY_SUCCESS) != null) { String res = json.getString(KEY_SUCCESS); if(Integer.parseInt(res) == 1){ //whole bunch of extra code! responseCode = 1; // Close Login Screen //finish(); }else{ responseCode = 0; // Error in login } } } catch (NullPointerException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return responseCode; } @Override protected void onPostExecute(Integer responseCode) { EditText userName = (EditText)activity.findViewById(R.id.emailEditText); EditText passwordEdit = (EditText)activity.findViewById(R.id.passEditText); if (responseCode == 1) { progressDialog.dismiss(); activity.loginReport(responseCode); userName.setText(""); passwordEdit.setText(""); //shared prefences, store name } if (responseCode == 0) { progressDialog.dismiss(); activity.loginReport(responseCode); } } </code></pre> <p>}</p> <p>Just follow along with the progressDialog. It starts up in the main class that launches the task, is passed to the AsyncTask. Then when the AsyncTask constructs, the progressDialog is stored as a field in the AsyncTask. Then, in preExecute, we show() the progressDialog. Then, depending on the result of doInBackground, we send a 1 or 0 to onPostExecute. That one or zero tells onPostExecute what to do, which in my code dismisses the dialog either way, but then runs different methods in the main class. Hope this helps you compare a similar task to your own. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload