I have a method "InitView", I want to call it in every Activity, I create a class "Helper" and in it my method I make it static and pass Activity like parameter.
I'm wondering if its the best solution and if there is no memory leak or something like that. here is my code:
public class Helper {
public static void intView(Activity activity) {
Window window = activity.getWindow();
Drawable background = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.gradient_bg, null);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(android.R.color.transparent));
//window.setNavigationBarColor(this.getResources().getColor(android.R.color.transparent));
window.setBackgroundDrawable(background);
}
}
CodePudding user response:
You can do this by Inheretance.
You can do a base Activity, that contains the initView function line of code.
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
}
private void initView(){
Window window = activity.getWindow();
Drawable background = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.gradient_bg, null);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(android.R.color.transparent));
//window.setNavigationBarColor(this.getResources().getColor(android.R.color.transparent));
window.setBackgroundDrawable(background);
}
}
You can extend this BaseActivity to your activities
public class YourActivity extends BaseActivity {
}
Now, you are 100% sure that no leak, and you get rid of static
keyword.