android - how to update the theme not in the onCre

2019-08-17 17:37发布

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条回答
爷的心禁止访问
2楼-- · 2019-08-17 18:23

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);
查看更多
登录 后发表回答