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