Question

When my app gets launched i am calling a Asyn Task function. In the background process its hitting 3 APIs and their parsing functions goes on, It will be taking around 10 minutes of time.

In that case if the user clicks on the android default back button, then the asyn task used to get cancelled. To avoid this i tried blocking the back button action as follows.

class First_Time_Sync extends AsyncTask<Void, Void, Void> 
     {
        ProgressDialog dialog1 = new ProgressDialog(Progressss.this);

        protected void onPreExecute() 
        {   
            backMode = true;
            dialog1.setMessage("Welcome \nThank you for waiting.");
            dialog1.setIndeterminate(true);
            dialog1.show();
        }       

        protected Void doInBackground(Void... arg0) 
        {
            .................                   
            return null;
        }

        protected void onPostExecute(Void unused) 
        {
            backMode = false;
            dialog1.dismiss();
        }
     }

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if (keyCode == KeyEvent.KEYCODE_BACK) 
        {
            if (backMode) 
            {
                return true;
            }
            else 
            {
                return super.onKeyDown(keyCode, event);
            }
        }       
        return super.onKeyDown(keyCode, event);
    }

Now when the async task function called and if the user clicks back button progress bar gets cancelled. But the background process goes on and the back button is been blocked until the background process completes.

I want the progress bar to be shown even if the back button is pressed, so that it may not get vanished.

How to do this......

Was it helpful?

Solution

You will have to set the cancelable property of the dialog to false

dialog1.setCancelable (false);

This will prevent back key from cancelling your dialog

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top