Android: click event after Activity.onPause()

I know this question is very old and far far away from being active. I came across this question via a blog.

A simple solution which I can think off is maintaining a flag globally in Activity A and setting it immediately inside onClick of Button A. This flag can be reset in onResume of Activity A.

This flag should be used at the top of onClick handler and all the clicks of all the views must be ignored. Of course, this requires that there is a single onClick method.

private boolean isOnClickIgnored= false;

@Override
public void onResume() {
    super.onResume();
    isOnClickIgnored = false;
}

@Override
public void onClick(View v) {
    super.onClick(v);
    if(isOnClickIgnored){
        return;
    }
    switch (v.getId()){
        case R.id.btnA:
            isOnClickIgnored = true;
            // Do button a things like opening Activity B
            break;
        case R.id.btnB:
            // Do button b things
            break;
        case R.id.btnZ:
            // Do button z things
            break;
    }
}

In OnClickListener of button A, disable the button b.

Button.setEnabled(false);

Just enable the button at the of A's onClickListener or at onResume depending on your requirements.


This whole concept can be easily solved using ViewModels + lifecycle-aware LiveDatas where LiveData expose events to UI-layer only when it's allowed.