开发者

Android GUI :: Continuous click events hangs

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开发者_C百科 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:

myImageButton.setClickable(false);

Update:

This is how events work in Android:

  1. When user clicks a view an event is added to the event queue.
  2. Events are processed on EDT in a serial fashion: one after another.
  3. Event enqueuing and event dispatching is done in separate threads: processing an event does not prevent enqueuing a new event.

A solution to your problem:

  1. You should process events as fast as possible and not block the EDT. This means you should do all long-running tasks (= your database operations) in the background thread - use AsyncTask for that.
  2. When you start a background task you should set a flag (backgroundWorkRunning) and clear it when done.
  3. When a new event is dispatched, first check the backgroundWorkRunning flag. Do nothing if flag is already set.


May be you can use a global flag like a boolean variale that is set to false by default and when it is false the addd entry function is executed. on entering the function set the flag to true. Hope this helps.


you can use a static boolean variable which you cab set true in its click event handling. when you click on that button mark it true and also apply an if block which checks weather the variable is true or not. when your task is done and the code flow is about to exist the onClickListener, you can again set that boolean variable to false.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜