简体   繁体   中英

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.

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);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM