In my project there is a main form with AsynkTask , on which I pull up 1 json. Loading other forms does not occur until the data arrives. Task: make it so that

  • Went out AlertDialog with the "Repeat" button.
  • When pressed, Alert would close, and a new download took place. Downloading is expressed in the opening of the progress dialogue.

There is an unfinished code fragment with a certain flag, which I updated after an error occurred. Removed too much, leaving only the essence. But the problem is that the alert itself did not close, and the progress bar blocked it when I resumed the download. For example, specifically turn off the Internet, open the application, caused a window. Then you turn it on, press it again, the application continues to work, having received the information, and a copy of the unclosed dialog still remains, and then closes itself, and once again sends a signal to download.

  @Override protected void onResume() { new ParseTask().execute(); super.onResume(); } private class ParseTask extends AsyncTask<Void, Void, String> { Button buttonContinue = (Button) (findViewById(R.id.buttonContinue)); @Override protected void onPreExecute() { super.onPreExecute(); showProgress(); } @Override protected void onCancelled() { super.onCancelled(); } @Override protected String doInBackground(Void... voids) { try { ... } catch (Exception e) { e.printStackTrace(); } return resultJson; } @Override public void onPostExecute(final String strJson) { super.onPostExecute(strJson); ObjectClass objectClass; ... .... try { .... } } catch (JSONException e) { e.printStackTrace(); errorDetecer = true; } AlertDialog.Builder builder = new lertDialog.Builder(MainActivity.this); if(errorDetecer) { new ParseTask().onCancelled(); builder.setTitle("Ошибка загрузки данных"); builder.setCancelable(false); builder.setMessage("Скорее всего, она связана с работой сервера или сети. Повторим операцию?"); builder.setNegativeButton("Повторить", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); alertDialog.dismiss(); onResume(); } }); errorDetecer = false; alertDialog = builder.create(); alertDialog.show(); } else { } buttonContinue.setVisibility(View.VISIBLE); hideProgress(); } } 

The first question is simple - is it worth it in my opinion to take a closer look at more modern technologies from the complexity of creating a dialogue? How old is the asynktask?

And second, who can share the bug tracking experience using the AlertDialog ?

  • one
    You can ping a server connection before sending a request. public boolean isConnected () throws InterruptedException, IOException {String command = "ping -c 1 -w 1 www.site.ru"; return (Runtime.getRuntime (). exec (command) .waitFor () == 0); } It is limited to one second, so nothing will hang. - Kirill Malyshev
  • one
    Your idea is generally working, the only blunder is to call onResume directly. This is a life cycle method; only the system can call it. This is correct and will be OK. - Eugene Krivenja

0