Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theme Programmatically set. How to reload Activity to apply

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?

like image 459
Leandros Avatar asked Feb 25 '12 17:02

Leandros


People also ask

How do you recreate current activity?

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.

How do I change my activity theme?

Open themes. xml (night) . In the Project pane select Android, go to app > res > values > themes > themes.

How do I apply a theme to my android?

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.


2 Answers

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

like image 66
Dante Avatar answered Nov 14 '22 23:11

Dante


Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
like image 26
sandi Avatar answered Nov 14 '22 21:11

sandi