Luk
Luk

Reputation: 396

Android How to set Flags global for all Activities

I have the problem that currently every time I start a new Activity for example like this:

Intent exchangedetail = new Intent(getActivity(), ExchangeDetail.class);
exchangedetail.putExtra("key", web[+ position]);
getActivity().startActivity(exchangedetail);

I have to set my Light NavBar and Status Bar every time like this:

//Setup Status Bar and Nav Bar white if supported
View decorView = getWindow().getDecorView();
Window win = getWindow();

if(Build.VERSION.SDK_INT >= 27) {
    decorView.setSystemUiVisibility(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS |
            View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
            View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
else if (Build.VERSION.SDK_INT >= 23 && Build.VERSION.SDK_INT < 27) {
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
else {
    win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}

Is there any way to for example set this flags in my MainActivity and ever new Intent/Activity use this flags also so I dont have to set them every time I call a new Intent? I think style.xml would work yes, but I need to set those flags promatically at StartUp from my App.

Upvotes: 1

Views: 1622

Answers (1)

Rajasekaran M
Rajasekaran M

Reputation: 2538

You can use base activity and can extend then on your every activity

public class BaseActviity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   //Setup Status Bar and Nav Bar white if supported
    View decorView = getWindow().getDecorView();
    Window win = getWindow();

    if(Build.VERSION.SDK_INT >= 27) {
        decorView.setSystemUiVisibility(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS |
                View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
                View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }
    else if (Build.VERSION.SDK_INT >= 23 && Build.VERSION.SDK_INT < 27) {
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
    else {
        win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

}
}

class MainActivty extends BaseActviity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
}

Upvotes: 2

Related Questions