解决部分android手机页面跳转的黑白屏、闪屏、显示桌面背景问题

  • 关于
  • 修改后的方案效果图
  • 问题思路及解决办法
  • 最终方案


关于

  今天在查看登录页面美观度的时候意外发现手上的oppo手机在页面跳转的时候会有明显的显示桌面背景的问题,先看一下有问题的页面跳转(此篇文章也会作为简易音乐博客系列之一):

Android 黑白主题 安卓黑白屏幕_java

修改后的方案效果图

Android 黑白主题 安卓黑白屏幕_页面跳转_02

  通过gif可以看到有一瞬间的手机背景的展示问题,当然了,黑白屏的问题在本文一开始就解决了:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowIsTranslucent">true</item>
        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
        <item name="android:statusBarColor" tools:ignore="NewApi">#00000000</item>
    </style>
 <style name="ThemeSplash" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentNavigation" tools:ignore="NewApi">true</item>
        <!--解决部分手机隐藏状态栏顶部出现小黑条的问题-->
        <item name="android:windowLayoutInDisplayCutoutMode" tools:ignore="NewApi">shortEdges</item>
        <!--解决白屏问题-->
        <item name="android:windowBackground">@color/colorAccent</item>
        <item name="android:windowFullscreen">true</item>
        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
        <item name="android:statusBarColor" tools:ignore="NewApi">#00000000</item>
    </style>

问题思路及解决办法

  上图gif里面总共三个页面,SplashActivity.javaLoginSelectActivity.javaLoginActivity.java,对应界面如下图:


Android 黑白主题 安卓黑白屏幕_android_03

Android 黑白主题 安卓黑白屏幕_Android 黑白主题_04

Android 黑白主题 安卓黑白屏幕_java_05

  我还特意为此去学习了下如何使图片并排显示。
其中第一个SplashActivity.java是启动页面,对应上面的ThemeSplash样式,跳转LoginSelectActivity.java页面代码如下:

//ARouter路由
 ARouter.getInstance()
                        .build(Config.ROUTE_LOGINSELECT)//跳转到选择按钮页面
                        .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation(SplashActivity.this);
                       finish();

  想着是不是因为跳转的时候清空了栈,导致页面是从桌面启动的,去掉Intent.FLAG_ACTIVITY_CLEAR_TASK标签:

//ARouter路由
 ARouter.getInstance()
                        .build(Config.ROUTE_LOGINSELECT)//跳转到选择按钮页面
                        .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation(SplashActivity.this);
                       finish();

  修改了之后还是显示了桌面背景,继续修改,想着给AppTheme添加一个不透明的背景呢,修改如下:

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowIsTranslucent">true</item>
        <!--解决页面跳转的状态栏白屏问题-->
        <item name="android:windowBackground">@color/colorAccent</item>
        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
        <item name="android:statusBarColor" tools:ignore="NewApi">#00000000</item>
    </style>

  修改跳转代码:

ARouter.getInstance()
                        .build(Config.ROUTE_LOGINSELECT)//跳转到选择按钮页面
                        .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation(SplashActivity.this);
                        //去掉finish();方法 因为跳转过程中销毁了前一个页面,导致页面显示了透明背景即显示了手机桌面背景

最终方案

  贴下最终完整代码吧,首先是样式styles.xml:

<resources xmlns:tools="http://schemas.android.com/tools">

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowIsTranslucent">true</item>
        <!--解决页面跳转的状态栏白屏问题-->
        <item name="android:windowBackground">@color/colorAccent</item>
        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
        <item name="android:statusBarColor" tools:ignore="NewApi">#00000000</item>
    </style>
    <!--启动页样式-->
    <style name="ThemeSplash" parent="AppTheme">
       <item name="android:windowTranslucentNavigation" tools:ignore="NewApi">true</item>
        <!--解决部分手机隐藏状态栏顶部出现小黑条的问题-->
        <item name="android:windowLayoutInDisplayCutoutMode" tools:ignore="NewApi">shortEdges</item>
        <item name="android:windowFullscreen">true</item>
    </style>

</resources>

SplashActivity的跳转:

ARouter.getInstance()
                        .build(Config.ROUTE_LOGINSELECT)//跳转到选择按钮页面
                        .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation(SplashActivity.this);
                overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);//转场动画