简体   繁体   English

重新创建主题更改活动

[英]Re-create an Activity on Theme change

What I Have 我有的

I have one main activity and one activity for settings. 我有一项主要活动和一项设定活动。 The user can move from the main activity to the settings. 用户可以从主要活动转到设置。 In the settings activity, the user can select a theme for the app. 在设置活动中,用户可以为应用选择主题。 Now I have one main activity for the app. 现在,我有一个针对该应用程序的主要活动。

When the user selects the theme, I have finished and restarted that activity to reflect changes immediately. 当用户选择主题时,我已经完成并重新启动该活动以立即反映更改。

But when the user presses the back button he is taken to the main activity. 但是,当用户按下“后退”按钮时,他将进入主要活动。

The Problem 问题

The onResume() method is called and not the onCreate() . 调用onResume()方法,而不调用onCreate() So the setTheme() method on the onCreate() doesn't also get called. 因此, onCreate()上的setTheme()方法也不会被调用。 So the theme change is not reflected here. 因此主题更改未在此处反映。 So what should I do to bring the theme change in the main activity . 因此,我该怎么做才能在main activity带来theme变化。

I basically need to ensure that the onCreate() of the main activity is always called when a theme change occurs. 我基本上需要确保在发生主题更改时始终调用main activityonCreate()

in onResume() method call the recreate() recreate link onResume()方法中调用recreate() 重新创建链接

@Override
protected void onResume() {
// TODO Auto-generated method stub
    ...........
    recreate();
}

Try using a boolean : 尝试使用boolean

public class MainActivity extends Activity {
    public boolean toBeRecreated;

    @Override
    public void onResume() {
        if (toBeRecreated) { recreate(); }
    }
}

Of course you need to set MainActivity.toBeRecreated = true when the user changes your theme. 当然,当用户更改主题时,您需要设置MainActivity.toBeRecreated = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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