I have an asynctasks. In oncreate view of a dialog fragment i am creating the object of my asynctask Like below (Sample code)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
cashInValidatorListner = new CashInValidatorListner(mSessionManager.getCustomerId(),mSessionManager.getPosId(), this);
}
Now in onclick i am executing the async taks
@Override
public void onClick(View v) {
if(v==ok)
{
if(mHomeActivity.mProgressDialog!=null && !mHomeActivity.mProgressDialog.isShowing()){
mHomeActivity.mProgressDialog.show();
}
cashInValidatorListner.execute();
}
}
I have added oncancellistner for this progressbar
@Override
public void onCancel(DialogInterface dialog) {
if(dialog==mProgressDialog)
{
mDialogExtraOptions.cashInValidatorListner.cancel(true);
Toast.makeText(getBaseContext(), "Task Cancled", Toast.LENGTH_SHORT).show();
}
}
The first time i cancel the async task it get cancelled howerver on executing it again it gives error saying can not execute task already executed .
When i tried creating the object at onclick each time user click ok button the problem is solved Like this
@Override
public void onClick(View v) {
if(v==ok)
{
if(mHomeActivity.mProgressDialog!=null && !mHomeActivity.mProgressDialog.isShowing()){
mHomeActivity.mProgressDialog.show();
}
cashInValidatorListner = new CashInValidatorListner(mSessionManager.getCustomerId(),mSessionManager.getPosId(), this);
cashInValidatorListner.execute();
}
}
Here it works fine ,My question why the async task was not executing when i was running it second time in first case ?