LoginSignup
1
2

More than 1 year has passed since last update.

【Android】Android12のスプラッシュスクリーン後にツールバーが二重表示される問題

Posted at

問題

Android12からスプラッシュスクリーンが標準実装されましたが、適当に対応したところAndroid12でのみツールバーが二重に表示されてしまったため解決策を残しておきます。
そもそも二重に表示されること自体殆ど無いと思いますが、テーマをActivity内からSetTheme()で切り替えたりしてる場合にこの問題が発生するようです。

解決策

StartActivity.kt

   override fun onCreate(savedInstanceState: Bundle?) {
        // val splashScreen = installSplashScreen() <-これを消すだけ
        setTheme(R.style.MainTheme)// ここでテーマを切り替える(今回は固定)
        super.onCreate(savedInstanceState)
        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)

     // 省略

コメント 2022-05-23 182156.png

installSplashScreen()はSplashScreenのアニメーションなどを持ったオブジェクトだそうで、アイコン一枚表示するだけなら無くても問題ありませんでした。
逆にこれがあるとAndroid12でSetTheme()が効かず(?)何故かSplashThemeのツールバーが表示されてしまいました(自分で実装したツールバーと二重に表示されてしまう)。

公式ドキュメントを見る限りSetTheme()ではなくSplashTheme内のpostSplashScreenThemeでメインのテーマを指定するのがセオリーっぽいですが、動的にテーマを変えたい場合はSetTheme()を使うしかないと思います。

AndroidManifest.xml
       <!--省略-->
       <activity
            android:name=".StartActivity"
            android:exported="true"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--省略-->

style/SplashTheme.xml

    <!--Splash theme-->
    <style name="SplashTheme" parent="Theme.SplashScreen">
        <item name="android:colorPrimary">@color/colorPrimary</item>
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="windowSplashScreenBackground">@color/colorBackground</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/splash_image</item>
        <item name="windowSplashScreenAnimationDuration">200</item>
        <!--<item name="postSplashScreenTheme">@style/MainTheme</item>--><!--SetTheme()するのでいらない-->
    </style>
    <!--省略-->

解決!

正攻法ではないと思うので何か問題があったりおかしなことをしていたら教えてもらえると嬉しいです。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2