I want setTheme
to an activity at runtime, I have search some solutions by google. someone said call setTheme
before onCreate and setContentView can works, the code section like
public void onCreate(Bundle savedInstanceState) { setTheme(android.R.style.Theme_Translucent_NoTitleBar); super.onCreate(savedInstanceState); ... setContentView(...) }
but it not works, I want to know, is there another solution can setTheme to activity?
OnCreate will only be called one time for each lifetime of the Activity. However, there are a number of situations that can cause your activity to be killed and brought back to life. Thus, onCreate will be called again.
The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.
OnCreate is only called once.
Just try this - set your theme after super.onCreate(savedInstanceState);
and before setContentView(...)
Like below code -
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(android.R.style.Theme_Translucent_NoTitleBar); // Set here setContentView(...) }
Actually this only worked for me if I set it before calling super.onCreate(savedInstanceState);
public void onCreate(Bundle savedInstanceState) { final int themeRes = getIntent().getIntExtra(EXTRA_THEME_ID, 0); if (themeRes != 0) setTheme(themeRes); super.onCreate(savedInstanceState); //ect... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With