我想知道如何使用导航架构组件实现闪屏.

Till now I have something like this

enter image description here

用户必须在ProfileFragment中首次设置其个人资料,并可以从ChatFragment中编辑其个人资料.

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

推荐答案

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.

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 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添加到您的活动中(这将显示用户单击App图标时的闪屏主题):

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

然后在MainActivity分钟内回到你的常规AppTheme小时,在onCreate小时前打super次电话:

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

Kotlin相关问答推荐

创建具有共同父类型的两种不同类型对象的列表的最有效方法是什么?

UByte范围. Min_UTE.. UByte.MAX_UTE不符合预期

Android前台服务 list —Altbeacon

在kotlin中使用List(mylist. size){index—TODO()}或Map迭代>

如何在Jetpack Compose中从领域查询中读取数据?

如何避免使用公共类实现内部接口

Kotlin中一个接口的实现问题

为何Kotlin标准库中的AND和OR函数不能像&&和||一样进行短路运算?

Kotlin 函数中接收者和参数的类型相同

测试协程和线程之间的差异,我在kotlin中使用线程时无法得到OOM错误

如何在 Kotlin 中不受约束?

Kotlin 协程中的 Dispatchers.Main 和 Dispatchers.Default 有什么区别?

kotlin-bom 库是做什么的?

在调用Kotlin数据类中的超类构造函数之前访问函数

如何获取Kotlin中变量的名称?

如何使用协调器布局和行为在CardView上完成此动画?

如何在使用Koin DI的活动之间共享同一个ViewModel实例?

内联 Kotlin 方法没有覆盖报告

在 IntelliJ Idea 中未为 Kotlin @ConfigurationProperties 类生成 spring-configuration-metadata.json 文件

可见性不适用于 Kotlin