导航架构组件 - 启动画面 [英] Navigation Architecture Component - Splash screen

查看:30
本文介绍了导航架构组件 - 启动画面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用导航架构组件实现启动画面.

I would like to know how to implement splash screen using Navigation Architecture Component.

直到现在我有这样的东西

Till now I have something like this

用户第一次必须在 ProfileFragment 中设置他的个人资料,并且可以从 ChatFragment 编辑他们的个人资料.

A user has to setup his profile in ProfileFragment for the first time and can edit their profile from ChatFragment.

我的问题是我不知道如何在导航后从堆栈中删除 SplashFragment.我看过条件导航,但不太明白.

My problem is I don't know how to remove SplashFragment from stack after navigation. I've seen conditional navigation but didn't quite understand.

推荐答案

当闪屏显示给用户几秒钟时,这是一种常见的误用,用户正在浪费时间看闪屏而他们已经可以与应用程序进行交互.取而代之的是,您应该尽快将他们带到他们可以与应用程序交互的屏幕上.因为之前的启动画面被认为是安卓上的反模式.但是后来谷歌意识到用户点击图标和你的第一个应用程序屏幕准备交互之间仍然存在短窗口,在此期间你可以显示一些品牌信息.这是实现启动画面的正确方法.

There is a common misuse of the splash screen when it is shown to the user for some amount of seconds, and users are wasting their time looking at the Splash screen while they could already interact with the App. Instead of that, you should get them ASAP to the screen where they could interact with the App. Because of that previously Splash screen was considered anti-pattern on android. But then Google realized that there still exist short window between user had clicked on the icon and your first App screen is ready for interaction, and during that time you can show some branding information. This is the right way to implement a Splash screen.

因此,要以正确的方式实现启动画面,您不需要单独的启动画面片段,因为它会在应用加载中引入不必要的延迟.为此,您只需要特殊的主题.理论上,App 主题可以应用于 UI 并在您的 App UI 初始化并变得可见之前变得可见.所以简而言之,你只需要像这样的 SplashTheme:

So to implement Splash screen the right way you don't need separate Splash Fragment as it will introduce an unnecessary delay in App loading. To do it you will need only special theme. In theory App theme could be applied to UI and becomes visible much sooner than your App UI will be initialized and become visible. So in a nutshell, you just need SplashTheme like this:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_background</item>
</style>

splash_background drawable 应该是这样的:

splash_background drawable should be like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
        android:opacity="opaque"> <!-- android:opacity="opaque" should be here -->
    <item>
        <!--this is your background, you can use color, gradient etc.-->
        <color android:color="@color/colorPrimary"/>
        <!--<shape>
              <gradient
                   android:angle="315"
                   android:endColor="#1a82ff"
                   android:startColor="#2100d3"
                   android:type="linear"/>
        </shape> -->
    </item>
    <item>
        <bitmap android:src="@drawable/ic_logo"
                android:gravity="center"/>
    </item>
</layer-list>

无论如何,您的片段都将托管在某个活动中,我们称之为 MainActivty.在 Manifest 中,只需将 SplashTheme 添加到您的 Activity(从用户单击应用程序图标的那一刻起就会显示启动画面主题):

Your fragments will be anyway hosted in some activity, let's call it MainActivty. In the Manifest just add SplashTheme to your Activity (that will show the splash screen theme from the moment user had clicked the App icon) :

<activity android:name=".ui.MainActivity"
              android:theme="@style/SplashTheme">

然后在 MainActivity 中返回到您的常规 AppThemeonCreate 中执行此操作,然后 super 调用:

Then in MainActivity to return to your regular AppTheme do this in onCreate before super call :

override fun onCreate(savedInstanceState: Bundle?) {
    setTheme(R.style.AppTheme)
    super.onCreate(savedInstanceState)
    .....

这篇关于导航架构组件 - 启动画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆