Theme Programmatically set. How to reload Activity

2019-03-28 17:38发布

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?

2条回答
闹够了就滚
2楼-- · 2019-03-28 18:02

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

查看更多
家丑人穷心不美
3楼-- · 2019-03-28 18:14
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
查看更多
登录 后发表回答