Ask PIN when app comes from Background

I am trying to implement a feature in which user is asked for PIN every time it opens an App or coming from Background. After searching, I found a solution.

I have base Activity class from which rests are inherited. It’s called ParentActivity. I implemented Foreground.Listener in my ParentActivity and override onBecameForeground, and opening PINActivity when it runs. Something like below

public abstract class ParentActivity extends AppCompatActivity implements Foreground.Listener {

    ...

    @Override
    public void onBecameForeground() {
        openSecurityActivity();
    }

    ...

}

And then override this method in all child classes to call super

@Override
public void onBecameForeground() {
    super.onBecameForeground();
}

PROBLEM

Now the problem is whenever App comes in foreground, this method is running the no of times as there are child classes which are overriding this method, even when child Activity is not created. I tried to put a check inside openSecurityActivity() like below,

synchronized protected void openSecurityActivity() {
    if(securityPinRequired) {
        if(!isSecurityActivityOpened) {
            isSecurityActivityOpened = true;

            Intent intent = new Intent(this, FingerprintAndSecurityPINActivity.class);
            intent.putExtra(Constants.ASK_SECURITY_PIN, true);
            intent.putExtra("isForBackgroundVerification", true);
            startActivity(intent);
        }
    }

}

but it’s still opening it twice. I can’t figure out the problem. Am I missing any basic OOP concept here?

Any help is appreciated. Thanks.

add android:launchMode=”singleInstance” into AndroidManifest.xml file