导航架构组件 - 启动画面

问题描述 投票:10回答:5

我想知道如何使用Navigation Architecture Component实现启动画面。

直到现在我有这样的事情

enter image description here

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

我的问题是我不知道如何在导航后从堆栈中删除SplashFragment。我见过conditional navigation但不太明白。

android kotlin splash-screen android-architecture-navigation
5个回答
3
投票

要实现正确的Splash屏幕,请按照@Sander指出的this教程进行操作。简而言之,要实现Splash屏幕,你需要像这样的SplashTheme

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

splash_background drawable应该是这样的:

<?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>

Manifest中,只需在您的活动中添加SplashTheme

<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)
    .....

14
投票

在Android上,启动画面一直很奇怪。您只想在单击应用程序图标和创建第一个Activity之间显示启动画面。如果您的启动画面是Fragment,用户仍然会看到白色背景,直到创建第一个Activity并且应用程序的启动时间将增加,因为必须创建并删除启动Fragment。最好的做法是在AppTheme中使用像Ian Lake(Android框架工程师)这样的启发this post

至于导航,您的应用应该有一个固定的目的地,这是用户在进入和退出您的应用时看到的第一个和最后一个屏幕,如principles of navigation中所述。在你的情况下,将ChatFragment作为固定目的地是有意义的。在onCreateChatFragment中,您应该检查用户是否已经拥有一个配置文件,并使用ProfileFragment将它们重定向到conditional navigation,如果他们没有。


7
投票

你可以尝试这个,它目前适用于我:

 <action
        android:id="@+id/action_splashFragment_to_profileFragment"
        app:destination="@id/signInFragment"
        app:launchSingleTop="true"
        app:popUpTo="@id/splashFragment"
        app:popUpToInclusive="true" />

只需设置popUpTo(the current destination)popUpToInclusive(true),这将弹出所有其他屏幕,直到它到达指定目的地 - 如果popUpToInclusive()设置为true,也会弹出目的地 - 然后导航到新目的地。


3
投票

这是解决上述情况的两种方法。

一:在qazxsw poi,覆盖qazxsw poi方法,如果当前NavHostFragment's ActivityonBackPress()然后只是NavDestination MainFragment

finish()

二:在Navigation_graph Activity@Override public void onBackPressed() { NavDestination navDestination = mNavController.getCurrentDestination(); if (navDestination != null && navDestination.getId() == R.id.mainFragment) { finish(); return; } super.onBackPressed(); } 中设置MainFragment操作

app:popUpTo="@id/nav_graph"

希望得到帮助!

警告:不推荐使用此功能。在导航alpha08中,clearTask标记被删除:app:popUpToInclusive="true"

在我的项目中,我在动作中测试应用程序:clearTask =“true”,它运行良好。~~~

<?xml version="1.0" encoding="utf-8"?>
<navigation
    android:id="@+id/nav_graph"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    app:startDestination="@id/splashFragment">

    <fragment
        android:id="@+id/splashFragment"
        android:name="xxx.fragment.splash.SplashFragment"
        android:label="fragment_splash"
        tools:layout="@layout/fragment_splash">
        <action
            android:id="@+id/action_splashFragment_to_mainFragment"
            app:destination="@id/mainFragment"
            app:enterAnim="@anim/anim_right_in"
            app:exitAnim="@anim/anim_left_out"
            app:popEnterAnim="@anim/anim_left_in"
            app:popExitAnim="@anim/anim_right_out"
            app:popUpTo="@id/nav_graph"
            app:popUpToInclusive="true"/>
        <action
            android:id="@+id/action_splashFragment_to_guideFragment"
            app:destination="@id/guideFragment"
            app:enterAnim="@anim/anim_right_in"
            app:exitAnim="@anim/anim_left_out"
            app:popEnterAnim="@anim/anim_left_in"
            app:popExitAnim="@anim/anim_right_out"
            app:popUpTo="@id/nav_graph"
            app:popUpToInclusive="true"/>
    </fragment>

    <fragment
        android:id="@+id/guideFragment"
        android:name="xxx.fragment.guide.GuideFragment"
        android:label="GuideFragment"
        tools:layout="@layout/fragment_guide">
        <action
            android:id="@+id/action_guideFragment_to_mainFragment"
            app:destination="@id/mainFragment"
            app:enterAnim="@anim/anim_right_in"
            app:exitAnim="@anim/anim_left_out"
            app:popEnterAnim="@anim/anim_left_in"
            app:popExitAnim="@anim/anim_right_out"
            app:popUpTo="@id/nav_graph"
            app:popUpToInclusive="true"/>
    </fragment>

    <fragment
        android:id="@+id/mainFragment"
        android:name="xxx.fragment.main.MainFragment"
        android:label="fragment_main"
        tools:layout="@layout/fragment_main">

    </fragment>

</navigation>

1
投票

你可以尝试这个技巧,这对我来说非常好。将启动画面设置为清单文件中的启动画面,然后从启动画面启动主机活动。

Remove the deprecated clearTask and launchDocument flags from NavOptions

在你的java文件中

<fragment
    android:id="@+id/splashFragment"
    android:name="xxx.SplashFragment"
    android:label="fragment_splash"
    tools:layout="@layout/fragment_splash">
    <action
        android:id="@+id/action_splashFragment_to_mainFragment"
        app:destination="@id/mainFragment"
        app:enterAnim="@anim/anim_right_in"
        app:exitAnim="@anim/anim_left_out"
        app:popEnterAnim="@anim/anim_left_in"
        app:popExitAnim="@anim/anim_right_out"
        app:clearTask="true"/>
</fragment>

主机活动的Xml代码

Manifest file code

`<activity android:name=".SplashScreen"
          android:theme="@style/Theme.AppCompat.Light.NoActionBar">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
</activity>`

而你导航将是这样的

         `new Handler().postDelayed(new Runnable(){
                    @Override
                    public void run() {
                        /* Create an Intent that will start the Navigation host activity . */
                        Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(mainIntent);
                        finish();
                    }
                }, 5000);`
© www.soinside.com 2019 - 2024. All rights reserved.