Android - Asynctask and Progressdialog

Asked

Viewed 487 times

0

Hey there, guys. I’m just starting in the Mobile area and I’m having some problems with the issue of user experience in my App.

In my App, it obtains a JSON from the server through a AsyncTask. Below a class well fulfilled, but that basically makes:

  • onPreExecute: Initiates a ProgressDialog.
  • doInBackground: Search the JSON on the server using OkHttp.
  • onPostExecute: Fills a List with the content of JSON and ends the ProgressDialog.

    class NetworkTask extends AsyncTask<Void, Void, String>{private ProgressDialog dialog;
    
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Resources resource = getResources();
        String aguarde = resource.getString(R.string.aguarde);
        String sincronizando = resource.getString(R.string.sincronizando_dados);
        dialog = ProgressDialog.show(rootView.getContext(), aguarde,sincronizando, true);
    }
    
    @Override
    protected String doInBackground(Void... params) {
        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(ServerUtil.URL_PROPRIETARIOS).build();
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            error += e.getMessage();
        }
        return null;
    }
    
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if(s != null){
            try {
                JSONArray jsonArray = new JSONArray(s);
                proprietarios = new Proprietarios(jsonArray);
                adapter = new ProprietarioAdapter(rootView.getContext(), proprietarios.getList(), gadoProprietario);
                lista.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
                error += e.getMessage();
            } catch(Exception e){
                e.printStackTrace();
            }
        }
        if(proprietarios != null && proprietarios.getList().size() > 0){
            for(Proprietario proprietario : proprietarios.getList()){
                new GadoProprietarioTask(proprietario.getId()).execute();
            }
        }
        dialog.dismiss();
    }
    

My doubt:

Ex.: I try to connect and receive JSON in less than 100ms, there is no need to open a ProgressDialog and be closed almost instantly. So I want to avoid this instant open and close feat I want it to start only after 200ms and if so 201ms it does not close immediately get over 200ms for example.

If the explanation is not good enough say that I redo, I thank you.

Obs.: AsyncTask is the best way for what I’m doing? If you know any better comment there!

1 answer

1


The less you keep the user interface busy, the better. Honestly, I don’t see this as a problem but I agree that this is going to be aesthetically awkward.

In this case, you will need to remove the ProgressDialog class NetworkTask and pass it to the scope that you are executing this task.

But not only that, you’ll also have to schedule a CountDownTimer or a Handler to fire the task after a certain interval.

Example using Handler

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        new NetworkTask().execute();
    }
}, 200);

Example using Timer

new Timer().schedule(new TimerTask() {          
    @Override
    public void run() {
        new NetworkTask().execute()
    }
}, 200);

Of course, don’t forget to display the ProgressDialog before the timer you created so that it appears before the execution of the task.

Also don’t forget to take into account that when we’re testing networking, usually the development server, may be faster to demand for n type factors, latency, server availability, amount of data to be consulted, finally. maybe you don’t even need to make this change to your app release.

I hope it helped.

  • 1

    Thanks, I had already found here some examples with Countdowntimer that meet my need, but you gave me the answer I was needing. There really is the issue of latency and N extra factors on the server and maybe not even necessary outside the development environment, but really I was bothered by the UX of Dialog kkkk. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.