About Android 12 splash screen page adaptation (1)

background

A cliche content, startup page adaptation.
Starting from Android 12, when you start the app, the system will thoughtfully give you a "system-level" startup page. It covers most of the original splash screen page events in many apps. As a result, the display time of many apps' customized splash screen pages is shortened, and even cannot be viewed, so adaptation is required.

environment

It has nothing to do with the environment. Your project can be run and
compiled. The compilation sdk needs to be 12 or above.

Ideas

According to official suggestions, there are the following solutions:
(1) Transfer all old businesses to the new adaptation startup page
(2) Use the old startup page (the effect is not very good)
This article will use the method (2) to achieve a compromise .

Implementation ideas

(1) Introduce splashscreen for all-round adaptation. Introduce implementation "androidx.core:core-splashscreen:1.0.0" in
build.gradle. In the startup page, before the setContentView() method, call the method: installSplashScreen() and then set the SplashScreen object. Listening, such as: setOnExitAnimationListener() setKeepOnScreenCondition()






setKeepOnScreenCondition is a switch that keeps the Android 12 system startup page. The code is as follows:

mSplashScreen.setKeepOnScreenCondition(object : 
			SplashScreen.KeepOnScreenCondition {
                override fun shouldKeepOnScreen(): Boolean {
                    Log.d("Splash", "Splash shouldKeepOnScreen")
                    return false
                }
            })

Return false means not to keep, return true means to keep. Under normal circumstances, when return false, the method:
setOnExitAnimationListener() will be triggered. In special cases, some system roms have castrated this part and there will be no such callback. This logic must also be included in the scope of adaptation.
Then define a theme to use as the startup page:

    <style name="Theme.Splash" parent="Theme.AppCompat.Light.NoActionBar">
        <!--启动画面背景颜色-->
        <item name="android:windowSplashScreenBackground">@color/purple_200</item>
        <!-- 启动画面icon图标:这里可以是图片、帧动画等-->
        <item name="android:windowSplashScreenAnimatedIcon">@color/purple_200</item>
        <!--        <item name="windowSplashScreenIconBackgroundColor">@null</item>-->
        <!-- icon动画在关闭之前显示的时长:最长时间为1000毫秒-->
        <item name="android:windowSplashScreenAnimationDuration">1000</item>
        <!-- Splash退出后的主题-->
        <item name="postSplashScreenTheme">@style/Theme.AndroidJiaguChannel</item>
    </style>

The core code is as follows:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 初始化操作(必须放在setContentView()之前)
        val needToShow = isNeedShowSplash()
        val mSplashScreen: SplashScreen? = if (needToShow) installSplashScreen() else null
        setContentView(R.layout.activity_splash)
        if (needToShow && mSplashScreen != null) {
            // Splash展示完毕的监听方法
            mSplashScreen.setOnExitAnimationListener(object : SplashScreen.OnExitAnimationListener {
                override fun onSplashScreenExit(splashScreenViewProvider: SplashScreenViewProvider) {
                    Log.d("Splash", "Splash onSplashScreenExit")
                    mHadExitAnim = true
                    splashScreenViewProvider.remove()
                    toMainActivity()
                }
            })
            mSplashScreen.setKeepOnScreenCondition(object : SplashScreen.KeepOnScreenCondition {
                override fun shouldKeepOnScreen(): Boolean {
                    Log.d("Splash", "Splash shouldKeepOnScreen")
                    return false
                }
            })

            MainScope().launch {
                delay(200)
                if (mHadExitAnim) {
                    return@launch
                }
                //没有,按普通逻辑执行
                mHadExitAnim = true
                toMainActivity()
            }
        } else {
            toMainActivity()
        }
    }

    private fun toMainActivity() {
        Handler(Looper.getMainLooper()).postDelayed(Runnable {
            startActivity(Intent(this@SplashActivity, MainActivity::class.java))
        }, 3000)
    }

    /**
     * 是否需要启动闪屏页
     * */
    fun isNeedShowSplash(): Boolean {
        return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
    }

There is a MainScope delay(200) here, which is used to adapt to the situation where setOnExitAnimationListener has no callback due to rom castration in some domestic systems. It is indispensable, otherwise some models cannot enter the next page.

(2) Use native adaptation
. The idea of ​​native adaptation is roughly the same as (1). The difference is that no third-party dependencies are introduced for adaptation. The system's startup page is only exited the moment the page starts drawing. The core code is as follows:

    private var mHadExitAnim = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash2)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            Log.d("Splash", "Splash 2 安卓12以上")
            splashScreen.setOnExitAnimationListener {
                Log.d("Splash", "Splash 2 setOnExitAnimationListener")
                it.remove()
                mHadExitAnim = true
                toMainActivity()
            }

            MainScope().launch {
                delay(200)
                if (mHadExitAnim) {
                    return@launch
                }
                //没有,按普通逻辑执行
                mHadExitAnim = true
                toMainActivity()
            }
        } else {
            Log.d("Splash", "Splash 2 安卓12以下")
            toMainActivity()
        }


    }

    private fun toMainActivity() {
        Handler(Looper.getMainLooper()).postDelayed(Runnable {
            startActivity(Intent(this@SplashActivityOnly, MainActivity::class.java))
        }, 3000)
    }

Summarize

These are the methods that are more common at present. For method (1), it is highly customizable, but it forces the use of the system’s splash screen page. For (2), it only reduces the duration of the system splash screen page.
The above are some ideas for adapting the startup page. Finally, the official link is attached:
https://developer.android.google.cn/guide/topics/ui/splash-screen/migrate?hl=zh-cn

that’s all-------------------------------------------------------------------------------------------

Guess you like

Origin blog.csdn.net/motosheep/article/details/132908312