我想知道如何使用导航架构组件实现闪屏.
Till now I have something like this
用户必须在ProfileFragment
中首次设置其个人资料,并可以从ChatFragment
中编辑其个人资料.
我的问题是我不知道如何在导航后从堆栈中删除SplashFragment
.我看了conditional navigation个,但不太明白.
我想知道如何使用导航架构组件实现闪屏.
Till now I have something like this
用户必须在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)
.....