Search code examples
androidbuttonandroid-asynctaskback

Remove Dialog and go back to previous activity while parsing data using AsyncTask ,when clicking Back button


i'm using AsyncTask to retrive data from RSS ,,i'm showing a progress Dialog in onpreExecute method,,parse Data in the DoInbackground method,,,and Removing the Dialog,,,showing the data in onPostExecute method,,every thing is ok..but i want to handle the backbutton ,,so if the user clicked the back button while parsing the data,he can go back..but until now if i pressed back nothing happen..:/

@Override
    protected void onPreExecute() {
        showDialog(DIALOG_PROGRESS);
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        feedParser = new activity1Parser(Bean.LINK);

        try {
            list = feedParser.parse();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }           
    }

    @Override
    protected void onPostExecute(Boolean result) {

            activity1.removeDialog(DIALOG_PROGRESS);

}

Solution

  • When you create your dialog (in onCreateDialog), make it cancelable and call setOnCancelListener with a listener that notifies your task that the dialog has been removed. Then in onPostExecute, remove the dialog only if it has not been removed.