Android: Stopping method to be called twice if already running.

Posted by user285831 on Stack Overflow See other posts from Stack Overflow or by user285831
Published on 2010-03-03T23:46:18Z Indexed on 2010/05/07 11:18 UTC
Read the original article Hit count: 143

Filed under:
|
|
|
|

I'm trying to prevent my application to call the same method twice in the event of a double-click, or if the user presses different buttons quickly, almost at the same time.

I have clickable Views, acting as buttons, that call the same method but passing different parameters. This is the call:

startTheSearch(context, getState(), what, where);

Inside this method I'm creating a new Thread, because it queries a web server for the result:

new Thread(new Runnable() {

 public void run() {

    progDiag = ProgressDialog.show(ctx, null, "Searching", true);
    getServerXML(context, what, where, searchIsCustom, mOffset);
       handler.sendEmptyMessage(0);
 }
 }).start();

The problem is that upon two quick clicks, the method is fired twice, two threads are created, and consequently two new activities are created. That makes my app crash.

When the methods are done, and we have the result from the server, we call the handler:

private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
try {
Intent i = new Intent(Golf.this, Result.class);

Bundle b = new Bundle();
b.putString("what", mWhat);
b.putString("where", mWhere);
b.putInt("offset", mOffset);
b.putBoolean("searchIsCustom", searchIsCustom);
i.putExtras(b);

startActivityForResult(i, Activity.RESULT_OK);
progDiag.dismiss();


} catch (Exception e) {
 Alerts.generalDialogAlert("Error", "settings", ctx);
}

}
};

I tried to have a global boolean variable called "blocked" initially set to false, creating a condition like:

if(!blocked){
 blocked = true;

 new Thread(new Runnable() {
  public void run() {

But this only seems to work on slower phones like the G1, I tried on Nexus and before it set blocked = true, the second request has was granted. So is there any way I can block the method being called if it's already running, or if the thread has started so it wont create a new one?

Please, I really need to fix this. I've been developing on Android for almost 2 months now, but I'm yet to tackle that bug. Thanks in advance.

© Stack Overflow or respective owner

Related posts about android

Related posts about methods