Fastest way to change theme during app startup

2020-07-06 09:07发布

Currently, I do provide 2 theme in my app, based on user last choice - dark theme and light theme.

During main activity start-up, I will do the following.

public class MyFragmentActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // getUserLastThemeChoice will either return R.style.Theme_My_Light or 
        // R.style.Theme_My_Dark.
        this.setTheme(getUserLastThemeChoice());

        super.onCreate(savedInstanceState);

In my AndroidManifest.xml, I have the following

<application
    ...
    android:theme="@style/Theme.My.Dark" >

Due to the hard-coded value in AndroidManifest.xml, if user previous choice is light theme, I will observed the following.

  1. App start up with dark theme, due to AndroidManifest.xml
  2. A while...
  3. App will change to white theme due to this.setTheme in main Activity

In there any way I can avoid such transition observation? That's it, just present to user directly, based on their last choice.

p/s Note, it wouldn't help much if you remove the theme information from manifest. As the system is going to use holo dark by default. You will still observe the transition, especially in slow device.

标签: android
2条回答
太酷不给撩
2楼-- · 2020-07-06 09:30

I know this is very old but maybe it still helps:

If you set the activity's theme to

android:theme="@android:style/Theme.Translucent.NoTitleBar"

it'll not show that initial screen. Instead it'll just take a bit more for the app to show anything at all.

查看更多
干净又极端
3楼-- · 2020-07-06 09:41

Hiding theme manipulation, particularly on app start up is tricky. Unfortunately there is no way to consistently remove that initial transition, because there's no telling how fast/slow the device running it is. Android will always show that blank background in the default theme before inflating the layout for the main activity.

But there is a trick to hiding it. A splash screen that appears when the user opens the app, and fades out after the initial activity has loaded. This way the splash screen hides the theme transition, without jarring the user. Splash screens receive some well deserved hate, but a situation like this does justify their use. Again, I don't know the specifics of your app, but you have to weigh the pros/cons of having a splash screen and smooth transition vs. no splash screen and a jarring transition.

There are loads of ways of implementing splash screens. In your case I would try to create a launcher activity, with a neutral theme, and apply a smooth animation for the transition between the splash screen and your main activity like so:

startActivity( new Intent( LauncherActivity.this, MainActivity.class ) );
overridePendingTransition( R.anim.fade_in, R.anim.fade_out );

And the animations:

<!-- fadeout -->
<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="300" />

<!-- fadein -->
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="300" />
查看更多
登录 后发表回答