android - how to update the theme not in the onCreate event?

限于喜欢 提交于 2019-12-12 00:32:13

问题


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


来源:https://stackoverflow.com/questions/11876094/android-how-to-update-the-theme-not-in-the-oncreate-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!