tejas tejas - 8 months ago 71
Android Question

How to prevent the activity from loading twice on pressing the button

What I am trying to figure out is to prevent the activity from loading twice if I press the button twice.

I have an activity which loads on click of a button, say

myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Load another activity

Now because the activity to be loaded has some thing to do with the webservice and other operations, it takes a little time for it to load. Of course, I have a loading view for this.
But before that, if I press the button twice, I can see the activity being loaded twice.

Do any one know how to prevent this?


In the button's event listener, disable the button and show another activity.

    Button b = (Button) view;

    Intent i = new Intent(this, AnotherActitivty.class);

Override onResume() to re-enable the button.

    protected void onResume() {

        Button button1 = (Button) findViewById(R.id.button1);