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

后端 未结 19 1941
情深已故
情深已故 2020-12-01 00:15

I am trying to prevent the activity from loading twice if I press the button twice instantly after the first click.

I have an activity which loads on click of a butt

19条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 00:37

    If you're using onActivityResult, you could use a variable to save state.

    private Boolean activityOpenInProgress = false;
    
    myButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        if( activityOpenInProgress )
          return;
    
        activityOpenInProgress = true;
       //Load another activity with startActivityForResult with required request code
      }
    });
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if( requestCode == thatYouSentToOpenActivity ){
        activityOpenInProgress = false;
      }
    }
    

    Works on back button pressed too because request code is returned on event.

提交回复
热议问题