0

Please let me ask a question. I have a asyntask and a dialog like :

private class myAsyncTaskton extends AsyncTask<Void, Void, Boolean> {
    Exception error;



    @Override
    protected void onPostExecute(Boolean result) {
        if(result){
            if(!data.equals("{\"Table\":[]}")){
                try {
                    parsejson();

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            else{
                final Dialog dialog = new Dialog(company_daily_report.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.dialog);
                TextView tvmessage = (TextView) dialog.findViewById(R.id.message);
                tvmessage.setText("Không có dữ liệu");
                Button btcancel = (Button) dialog.findViewById(R.id.buttoncancel);
                dialog.setCanceledOnTouchOutside(false);
                btcancel.setText("OK");
                btcancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        ####HERE I WANT STOP ASYNSTACK#######

                    }
                });


            dialog.show();
            }

        } else {
            if (error != null) {
                final Dialog dialogeror = new Dialog(company_daily_report.this);
                dialogeror.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialogeror.setContentView(R.layout.dialog);
                TextView tvmessage = (TextView) dialogeror.findView
            }
        }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //dang xu li
    }

    @Override
    protected Boolean doInBackground(Void... params) {

        try{
            getdata();
            return true;
        } catch (Exception e){
            error =e;
            return false;

        }

    }
}

When run this code, i received dialog. I want stop asyntask from button dialog click event? How do that ? I try cancel(true), return ,.. but not working .

AndroidLTG
  • 91
  • 1
  • 11
  • [http://stackoverflow.com/questions/18045917/android-stop-asynctask](http://stackoverflow.com/questions/18045917/android-stop-asynctask) – M D Oct 20 '15 at 08:47
  • I think when you see the dialog, your task is already over. – invisbo Oct 20 '15 at 08:55
  • @invisbo you right.it over,but i received two dialog same@@. How to dismiss two dialog in one click ? – AndroidLTG Oct 20 '15 at 09:03

2 Answers2

0

You have to declare your dialog outside the onPostExecute() method....where you have declared Exception....

Now in doInBackground() method do this:

dialog.show();

and in onPostExecute():

dialog.dismiss();

or u can dismiss it using button in the dialog itself....

Chordin4tion
  • 984
  • 6
  • 15
0

You should restructure your code

private class myAsyncTaskton extends AsyncTask<Void, Void, Boolean> {
    Exception error;
    Dialog dialog = new Dialog(company_daily_report.this);
    Dialog dialogeror = new Dialog(company_daily_report.this);
    TextView tvmessage = (TextView) dialog.findViewById(R.id.message);
    Button btcancel = (Button) dialog.findViewById(R.id.buttoncancel);


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //dang xu li
    }


    @Override
    protected Boolean doInBackground(Void... params) {

        try{
            getdata();
            return true;
        } catch (Exception e){
            error =e;
            return false;

        }

    }


    @Override
    protected void onPostExecute(Boolean result) {
        if(result){
            if(!data.equals("{\"Table\":[]}")){
                try {
                    parsejson();

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            else{

                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.dialog);                   
                tvmessage.setText("Không có dữ liệu");                    
                dialog.setCanceledOnTouchOutside(false);
                btcancel.setText("OK");
                btcancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        ####HERE I WANT STOP ASYNSTACK#######

                    }
                });


            dialog.show();
            }

        } else {
            if (error != null) {
                dialogeror.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialogeror.setContentView(R.layout.dialog);
                //You can still use your tvmessage.setText("value") here
            }
        }
    }

}
Tosin Onikute
  • 3,883
  • 6
  • 38
  • 61