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

后端 未结 19 1958
情深已故
情深已故 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条回答
  •  萌比男神i
    2020-12-01 00:23

    You could just override startActivityForResult and use instance variable:

    boolean couldStartActivity = false;
    
    @Override
    protected void onResume() {
        super.onResume();
    
        couldStartActivity = true;
    }
    
    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        if (couldStartActivity) {
            couldStartActivity = false;
            intent.putExtra(RequestCodeKey, requestCode);
            super.startActivityForResult(intent, requestCode, options);
        }
    }
    

提交回复
热议问题