I am trying to prevent my application from using 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 interactive views that act like buttons that call the same method but pass different parameters. It's a challenge:
startTheSearch(context, getState(), what, where);
Inside this method, I create a new thread because it requests the 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 with two quick clicks, the method starts twice, two threads are created and, therefore, two new actions are created. This causes my app to crash.
When the methods are executed, 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);
}
}
};
"", false, :
if(!blocked){
blocked = true;
new Thread(new Runnable() {
public void run() {
, , , G1, Nexus , block = true, . - , , , ?