0

i'm trying to add a indeterminate progressDialog to my UI once i click a list element that calls a AsyncTask, but unfortunately, if I call the dialog on the onPreExecute like this:

@Override
protected void onPreExecute() {
    super.onPreExecute();
    dialog.setProgressStyle(R.style.AppTheme_Dark_Dialog);
    dialog.setIndeterminate(true);
    dialog.setMessage(activity.getString(R.string.Buscando));
    dialog.show();
}

Obs: Worth noting that the Async is not a subClass of the Activity Class, i'm passing the activity as a parameter to the constructor.

and

@Override
protected void onPostExecute(BuscaPendentesFechamento buscaPendentesFechamento) {
    super.onPostExecute(buscaPendentesFechamento);
    if(dialog.isShowing())
        dialog.dismiss();
}

the dialog simply doesn't show at all, although it is created and called(alredy checked, the activity instance is correct)

and if I set the dialog on the activity itself, like:

final ProgressDialog progressDialog = new ProgressDialog(RegistraPeso.this, R.style.AppTheme_Dark_Dialog);
    progressDialog.setIndeterminate(true);
    progressDialog.setMessage(getString(R.string.Buscando));
    progressDialog.show();
    BuscaPendentesFechamento exportProjectRequisition = new BuscaPendentesFechamento(getApplicationContext());
    response = exportProjectRequisition.execute(nomeEquip,RegistraPeso.this);
    progressDialog.dismiss();

the Dialog shows on the Ui, but only after the AsyncTask alredy performed, and not before it is called, and according to the code, it should be showing before the Async even gets created.

What can I do? What am I doing wrong? Please help.

Edit: Added where the Dialog is created:

private ProgressDialog dialog;
private RegistraPeso activity;
BuscaPendentesFechamento(RegistraPeso activity) {
    this.dialog = new ProgressDialog(activity);
    this.activity = activity;
}

Edit: Added the doInBackground:

    @Override
protected BuscaPendentesFechamento doInBackground(String... params) {
    ArrayList<UmData> jsonArrayResponse = JSONParser.makeHttpRequestArray(
            Constants.URL_PENDENTES_FECHAMENTO,
            Constants.METHOD_POST, requisition.writeJSON(params[0]),
            requisition.getContext());
    if(jsonArrayResponse!=null)
    requisition.setJsonArrayResponse(jsonArrayResponse);
    else {
        UmData umData = new UmData();
        umData.setItemUm("Server not responding");
        jsonArrayResponse = new ArrayList<>();
        jsonArrayResponse.add(umData);
        requisition.setJsonArrayResponse(jsonArrayResponse);
    }
    return requisition;
}
Carlos Mion
  • 37
  • 1
  • 8
  • 1
    Post your code where you passing context to AsycTask . Cause i do not see creation of dialog in onPreExecute(). – ADM Dec 04 '17 at 12:51
  • Have you checked other answers like [this](https://stackoverflow.com/a/4538370/2910520) or [this](https://stackoverflow.com/a/3893691/2910520)? – MatPag Dec 04 '17 at 12:53
  • second solution is useless, because it is not doing what it is worth for. – Ankit Patidar Dec 04 '17 at 12:53
  • Possible duplicate of [progressDialog in AsyncTask](https://stackoverflow.com/questions/4538338/progressdialog-in-asynctask) – MatPag Dec 04 '17 at 12:54
  • @ADM added the code – Carlos Mion Dec 04 '17 at 12:55
  • @MatPag Yes, thanks anyway, but I've searched the whole of the internet after this solution before asking here :( – Carlos Mion Dec 04 '17 at 12:56
  • Nothing to do with your issue but i would use a WeakReference to store my activity in the asynctask, it"s dangerous – DennisVA Dec 04 '17 at 12:57
  • @PrisonMike sorry, couldn't understand your point, you would use something dangerous? If i got it correct, what is the possible substitute? – Carlos Mion Dec 04 '17 at 12:58
  • If AsyncTask has a longer lifetime than it should for several possible reasons, you are leaking your activity use WeakReference – DennisVA Dec 04 '17 at 13:00
  • @PrisonMike Thanks! – Carlos Mion Dec 04 '17 at 13:03
  • Post the `doInBackground()` method. – Mike M. Dec 04 '17 at 13:08
  • @MikeM. posted! – Carlos Mion Dec 04 '17 at 13:26
  • That doesn't seem like a very heavy task. Are you sure it's not just completing too quickly for you to notice the dialog showing? – Mike M. Dec 04 '17 at 13:32
  • yes, the task calls a webService that calls a C++ Program that acess the dataBase, it takes sort of 3 seconds for the data to be returned to me, and through this 3 seconds the UI keeps frozen, So I wanted to show something for the user not to think the App froze :/ – Carlos Mion Dec 04 '17 at 13:33
  • 1
    Wait. How is the UI freezing? That's part of the reason to use an `AsyncTask`; so the UI doesn't freeze. Are you calling `get()` in your actual code? – Mike M. Dec 04 '17 at 13:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160431/discussion-between-carlos-mion-and-mike-m). – Carlos Mion Dec 04 '17 at 13:40

2 Answers2

0

Application context would not work in this case . Replace line below

 BuscaPendentesFechamento exportProjectRequisition = new BuscaPendentesFechamento(getApplicationContext());

To

 BuscaPendentesFechamento exportProjectRequisition = new BuscaPendentesFechamento(YourActivity.this);

I see your code you are calling dismiss() just after execute(). Remove this line.

 progressDialog.dismiss();
ADM
  • 20,406
  • 11
  • 52
  • 83
  • Thanks, but it changed nothing :( – Carlos Mion Dec 04 '17 at 12:57
  • that is not the problem, in the case of the second option, which is where i do ths dismiss() call right afther the execute(), the dialog never stops showing! – Carlos Mion Dec 04 '17 at 13:02
  • You probably messed up somewhere in code . Activity reference as global in asynctask . Code above should work .thx – ADM Dec 04 '17 at 14:15
0

OP here, the problem was solved by changing the code based on [this][1]

[1]: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? post here, sent to me by @MikeM, The problem was that I was getting the response using the .get() from the AsyncTask, and it was transforming the Async into a Syncronous Task. Thanks for the support Mike!

Carlos Mion
  • 37
  • 1
  • 8