1

Im calling a function in my activity, which is basically an asynchronous task to fetch data from a remote server via webservice.

 private void myAsyncTask() {
            new AsyncTask<Object, Object, Object>() {

                @Override
                protected void onPreExecute() {

                    progress_Dialog = ProgressDialog.show(a, "", "Loading");

                }

                @Override
                protected Integer doInBackground(Object... params) {


                    try
                    {
                    try {

                        MenuService menuService = new MenuServiceImpl();

                        MenuServiceResponse partnerMenu;
                            partnerMenu = menuService.getMenu();

                        productlist=Menu.getMenu().getMenuEntries();

                        System.gc();
                        return 0;
                    } catch (myServiceException e) {
                        bgFlag=true;

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

                        bgFlagForserviceExeption=true;
                        e.printStackTrace();


                    }
                    }                           return 0;
                }


                @Override
                protected void onPostExecute(Object result) {
                    if (progress_Dialog != null) {

                        progress_Dialog.dismiss();

                    }

                    try
                    {

                        if(bgFlagForserviceExeption)
                        {
                            MyAlertDialog.ShowAlertDialog(ShopActivity.this, "", "Please try again later", "OK");

                        }
                        if(bgFlag==false)
                        {
                     adapter = new ShopAdapter(
                            ShopActivity.this, productlist);

                                    allproduts.setAdapter(adapter);
                        }
                        else
                        {
                            MyAlertDialog.ShowAlertDialog(ShopActivity.this, "", " Please try again later", "OK");

                        }


                    }
                    catch(Exception e)
                    {
                        adapter=null;
                }

                }

            }.execute();



}

When i call this activity the progress bar will be shown until the doinbackground() ends.How can i exit from the background process on pressing phones back button,Right now the problem is i will have to wait until the background process completes even if i press the back button.How can i achieve this

Nikhil
  • 16,194
  • 20
  • 64
  • 81
playmaker420
  • 1,527
  • 4
  • 27
  • 52
  • 1
    you can't do this with `doInBackground` like this ... `AsyncTask.cancel` can only help then you are doing something in the loop in `doInBackground` and at every iteration you're checking if isCancelled() and then break the loop ... – Selvin Oct 25 '12 at 10:40
  • 1
    more explenation(on why `AsyncTask.cancel` has no apply here) ... in fact there is a pretty big chance that you're code "stuck" in `getMenu()` or `getMenuEntries()` methods and from `doInBackground` you have no possibilities to stop em ... – Selvin Oct 25 '12 at 11:02
  • 1
    http://stackoverflow.com/questions/6356956/onbackpressed-doesnt-trigger-when-showing-progressdialog – Selvin Oct 25 '12 at 12:26
  • check this one :http://stackoverflow.com/a/6931092/1405120 – ThePCWizard Oct 25 '12 at 12:30
  • @Selvin ..Thanks for the link and explanation http://stackoverflow.com/questions/6356956/onbackpressed-doesnt-trigger-when-showing-progressdialog This link helped me. – playmaker420 Oct 26 '12 at 07:31

1 Answers1

1

Override the back button.

Here it is

In that simply call asynctack.cancel(true);

Read here

Community
  • 1
  • 1
Abhi
  • 8,935
  • 7
  • 37
  • 60
  • 3
    Dear Users who are giving negative votes, Can you give reason? So that i can improve in my next post – Abhi Oct 25 '12 at 10:41
  • the same as other one and as i wrote in the comment under question asynctack.cancel(true); will not work – Selvin Oct 25 '12 at 10:46