Umit Kaya
Umit Kaya

Reputation: 5951

How to prevent method to be called twice?

I want to check programmatically whether the function is called or not. If it is called before, then do not allow and ignore to be called for the second time, if not then let button to call. Why i need to do this is because of i have a longClick button to run certain functionality, if user forgot to do this then however user will click another button which i will place the same functionality. I need to prevent running same function for twice.

public class ActivityA extends Activity{

private static boolean isScreenShotTaken = false;

blankcard.setOnLongClickListener(new OnLongClickListener() {

       if(!isScreenShotTaken)
            {
                Bitmap bitmap = takeScreenshot();
                saveBitmap(bitmap);
                isScreenShotTaken = true;

            }

        return false;
        }
});



btnsend.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

Intent intent = new Intent(Templates.this, MailCard.class);
intent.putExtra("name", Name);

if(!isScreenShotTaken)
            {
                Bitmap bitmap = takeScreenshot();
                saveBitmap(bitmap);
                isScreenShotTaken = true;
            }

startActivity(intent);             

        }
    });


public Bitmap takeScreenshot() {

// code exist here
//if called once, second call ignore

}

public void saveBitmap(Bitmap bitmap) {

// code exist here
//if called once, second call ignore

  }
}

Thank you for suggestions.

Upvotes: 2

Views: 7577

Answers (4)

iceyb
iceyb

Reputation: 436

Why dont you use a global flag variable?? something like,

btnsend.setOnClickListener(new View.OnClickListener() {

    @Override
    if(flag == true)
  {
    public void onClick(View v) {
        // TODO Auto-generated method stub

//          Bitmap bitmap = takeScreenshot();
//          saveBitmap(bitmap);

            flag = 1;

    }
  }
});

So even though setOnClickListener is called, it wont execute the code within.

Upvotes: 1

Phantômaxx
Phantômaxx

Reputation: 38098

My version:

I'd write it so:

// In the declaration section of your class.
private static boolean isScreenShotTaken = false;

// In the code.
btnsend.setOnClickListener
(
    new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if(!isScreenShotTaken)
            {
                Bitmap bitmap = takeScreenshot();
                saveBitmap(bitmap);
                isScreenShotTaken = true;
            }
        }
    }
);

blankcard.setOnLongClickListener
(
    new OnLongClickListener()
    {
        @Override
        public boolean onLongClick(View arg0)
        {
            if(!isScreenShotTaken)
            {
                Bitmap bitmap = takeScreenshot();
                saveBitmap(bitmap);
                isScreenShotTaken = true;
            }
        }
    }
);

Upvotes: 1

Zar E Ahmer
Zar E Ahmer

Reputation: 34360

If you have a button

once the button is pressed on .. In onclickListener

button.setclickable(false);

then button can't be pressed .

and if you have some method. than take a gloabal boolean variable boolean isFirstTime= false;

and once you get into the method set it to true;

 isFirstTime= true;

Upvotes: 0

Budius
Budius

Reputation: 39836

simple:

public Bitmap takeScreenshot() {
// before start
blankcard.setEnabled(false);
btnsend.setEnabled(false);

// do your sutff
... here ...

// at the end
blankcard.setEnabled(true);
btnsend.setEnabled(true);

}

if you're using an AsyncTask or similar, put those calls in preExecute and postExecute

Upvotes: 0

Related Questions