1

I am trying to make my first app in android studio and I have an issue, I have 2 activities and in both I have

public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        Log.e(TAG, "onWindowFocusChanged()");
        if (hasFocus) {
            hideSystemUI();
        }
    }
    private void hideSystemUI() {
        Log.e(TAG, "hideSystemUI()");
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
    }

But, when I change my activity to another one, the function is called from both activities, I don't want to destroy activity.

Should I make a third activity that contains this code and then remove from last two activities?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
WoW Boy
  • 13
  • 2

1 Answers1

0

This is a really, really common problem!

One solution is to create a BaseActivity that contains the shared code. Your two activities can then both extend BaseActivity to gain that functionality.

If you need more help with extending classes, this answer covers the same problem.

Jake Lee
  • 7,549
  • 8
  • 45
  • 86