Hello
I have one ImageButton with single image. I have added onclick event and normally it works fine.When Imagebutton is clicked my add entry function works.
Now when user continously clicks my imagebutton, My add entry functions executes that many times with same data. I want to prevent it. Such that, Imagebutton should not queue up to process next click event until my function gets executed completely.
I tried to myImageButton.setEnable(false) as soon as OnClick event executes. And Doing
myImageButton.setEnable(true) after my data entry function.
I also tried to put this code in myImageButton.isEnabled() but didnt work.
How to ignore such queued click events? Is there any other way (than setEnable()) to ignore/eat click processing?
I have checked by putting println statements that each click event is in sync…means all code executes in order.
EDIT
private OnClickListener m_AddClickHandler = new OnClickListener()
{
public void onClick(View v)
{
if(m_bDoAdd)
{
m_bDoAdd = false;
new AddTask().execute();
}
else
logData("Add::OnClick::not clicked");
}
};
private class AddTask extends AsyncTask<Void, Void, Integer>
{
@Override
protected Integer doInBackground(Void... params)
{
logData("doinbg, start"+m_bDoAdd);
int iStatus =Add(m_data);
logData("doinbg, start end, status="+iStatus+"flag="+m_bDoAdd);
return iStatus;
}
@Override
protected void onPostExecute(Integer result)
{
logData("onpostExec, start"+m_bDoAdd);
int iStatus = result;
if (iStatus == 0)
{
Toast.makeText(getApplicationContext(), R.string.strAdded, Toast.LENGTH_SHORT).show();
}
else if (iStatus == 1)
{
Toast.makeText(getApplicationContext(), R.string.strAlreadyExists, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), R.string.strAddFailed, Toast.LENGTH_SHORT).show();
}
m_bDoAdd = true;
logData("onpostExec, end"+m_bDoAdd);
}
}
void Add()
{
// Add info to db (takes few msecs)
}
I am still not getting “Add::OnClick::not clicked” in log.
Any further clue?
Try this inside onClick handler:
Update:
This is how events work in Android:
A solution to your problem:
backgroundWorkRunning) and clear it when done.backgroundWorkRunningflag. Do nothing if flag is already set.