导航体系结构组件-初始屏幕 [英] Navigation Architecture Component - Splash screen

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

问题描述

我想知道如何使用导航体系结构组件实现启动屏幕.

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.

推荐答案

在向用户显示启动画面几秒钟后,启动画面通常会被滥用,并且用户浪费时间在启动画面上他们已经可以与该应用进行互动了.取而代之的是,您应该尽快将它们带到可以与应用程序交互的屏幕上.因此,以前的Splash屏幕在Android上被视为反模式.但随后Google意识到,用户单击图标与您的第一个应用程序屏幕之间尚存在一个短窗口,可以进行交互,在此期间,您可以显示一些品牌信息.这是实现启动屏幕的正确方法.

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加载过程中引入不必要的延迟.为此,您只需要特殊的主题.从理论上讲,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添加到您的活动"(将从用户单击应用"图标的那一刻起显示初始屏幕主题):

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中返回到常规的AppTheme,请在super调用之前在onCreate中执行此操作:

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天全站免登陆