Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 783347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:31:57+00:00 2026-05-14T20:31:57+00:00

I am trying to prevent my application calling the same method twice in the

  • 0

I am trying to prevent my application calling 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:31:58+00:00Added an answer on May 14, 2026 at 8:31 pm

    Instead of using the “blocked” Boolean, you might investigate using a mutex. They’re designed for this kind of problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add an event listener to my air application that would prevent
I am trying to find a solution to prevent race conditions in my application
I'm trying to prevent user from inserting * in a textbox. This is what
I am trying to prevent a link in my jQTouch application from loading the
In a Grails application, I am trying to prevent the creation of cycles in
I'm building a Codeigniter application and I'm trying my hardest to prevent SQL injections.
I see that when calling the Application.Documents.Open method in word vsto there is a
I am trying to prevent multiple simultaneous logins in my web application. I want
Trying to prevent the application from shutting down when clicked the X button, it
I am trying to prevent the user from resizing (including maximizing) the window of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.