How to use superclass in static method with given Activity parameter?

hyperapps19 :

I `m trying to create static class ActivitySetup that is used to set language, theme, etc. for my activities. I have a problem with setting theme. Now i have following code:

     static void configureTheme(Activity activity, int defaultTheme) {
     String theme = PreferenceManager.getDefaultSharedPreferences(activity).getString("theme", "light");
     assert theme != null;

     switch (theme) {
         case "light":
             activity.setTheme(R.style.AppTheme);
             break;
         case "dark":
             activity.setTheme(R.style.Theme_AppCompat);
             break;
         default:
             activity.setTheme(defaultTheme);
             break;
     }
 }

But it crashes. I know that i should use super (of activity).setTheme instead of activity.setTheme, but how can i do that? How to pass instance of superclass as parameter to a static method?

Yagnesh Lashkari :

switch between themes dynamically you simply need to call setTheme before super.onCreate

public void onCreate(Bundle savedInstanceState) {
    setTheme(android.R.style.Theme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=117538&siteId=1
Recommended