How can I apply the Theme without restarting the whole App? If I do it with startActivity(getIntent()); finish();
the Activity quits and dont restart. Is it possible to simply restart / recreate the Activity to apply the theme?
This example demonstrates how do I restart an Activity in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Open themes. xml (night) . In the Project pane select Android, go to app > res > values > themes > themes.
To change default themes go to File and click on Settings. A new Settings dialog will appear, like this. Under the Appearance & Behaviour -> Appearance, you will find Theme. Choose the right theme from the drop-down and click on Apply and then Ok.
It is in the incorrect order.
finish();
intent = new Intent(this, <your_activity>.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This is the correct order.
Theme can be set before super.onCreate(savedInstanceState);
is being called. You need to destroy the activity and create it again and call immediately setTheme(THEME);
in onCreate()
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
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