android - how to update the theme not in the onCre

2019-08-17 17:49发布

问题:

In my android app, I set the theme like this:

@Override
public void onCreate(Bundle savedInstanceState){
    ThemeSetterActivity.setStyle(Main_MenuActivity.this); // this just calls context.setTheme();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);
}

But how do I change the theme using

ThemeSetterActivity.setStyle(Main_MenuActivity.this);

when it's in the onresume event. When I try it, it does call the function but the theme doesn't change. Does it have something to do with not calling:

super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);

回答1:

To change the theme of your application during run time, you can use the setTheme(...) method within the activity. You must set the theme to an activity, before loading the views of that activity.

For more info and implementation, refer the link:

Updated

And, as per the docs to set theme you need to restart the entire activity. You can try this code in onResume() of your activity,

Intent i = getIntent();
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);