Yas*_*yan 13 android splash-screen kotlin android-architecture-navigation
我想知道如何使用Navigation Architecture Component实现启动画面.
直到现在我有这样的事情
用户必须ProfileFragment首次设置其个人资料,并可以编辑他们的个人资料ChatFragment.
我的问题是我不知道如何SplashFragment在导航后从堆栈中删除.我见过条件导航但不太明白.
San*_*der 14
在Android上,启动画面一直很奇怪.您只想在单击应用程序图标和创建第一个图标之间显示启动画面Activity.如果您的启动画面是Fragment用户仍然会看到白色背景,直到Activity创建第一个,并且应用程序的启动时间将增加,因为Fragment必须创建并删除启动.最好的做法是使用AppTheme像Ian Lake(Android框架工程师)这样的文章解释.
至于导航,您的应用应该有一个固定的目的地,这是用户在进入和退出您的应用时看到的第一个和最后一个屏幕,如导航原理中所述.在你的情况下,制作ChatFragment固定目的地是有意义的.在onCreate的ChatFragment,你应该检查,如果用户已经有一个配置文件,将其重定向到ProfileFragment使用条件导航,如果他们不这样做.
您可以尝试一下,它目前适用于我:
<action
android:id="@+id/action_splashFragment_to_profileFragment"
app:destination="@id/signInFragment"
app:launchSingleTop="true"
app:popUpTo="@id/splashFragment"
app:popUpToInclusive="true" />
Run Code Online (Sandbox Code Playgroud)
只需设置popUpTo(the current destination)和popUpToInclusive(true),这将弹出所有其他屏幕,直到到达指定的目的地为止-如果popUpToInclusive()将其设置为true- 也会弹出目的地,然后导航到新的目的地。
这里有两种方法可以解决上述情况。
一个?在NavHostFragment's Activity,覆盖onBackPress()方法,ADN如果当前NavDestination是MainFragment那么刚刚finish()的Activity。
@Override
public void onBackPressed() {
NavDestination navDestination = mNavController.getCurrentDestination();
if (navDestination != null
&& navDestination.getId() == R.id.mainFragment) {
finish();
return;
}
super.onBackPressed();
}
Run Code Online (Sandbox Code Playgroud)
二:在 Navigation_graph 中设置为 MainFragment 动作app:popUpTo="@id/nav_graph"和app:popUpToInclusive="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>
Run Code Online (Sandbox Code Playgroud)
希望得到帮助!
警告?这已被弃用。在 Navigation alpha08 中,删除了 clearTask 标记:从 NavOptions 中删除已弃用的 clearTask 和 launchDocument 标志
在我的项目中,我在action中测试了app:clearTask="true",效果很好~~~
<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>
Run Code Online (Sandbox Code Playgroud)
当向用户显示初始屏幕达几秒钟时,通常会滥用初始屏幕,并且用户在已经可以与应用程序交互的同时浪费时间在初始屏幕上。取而代之的是,您应该尽快将它们带到可以与应用程序交互的屏幕。因此,以前的Splash屏幕在Android上被视为反模式。但随后Google意识到,用户单击图标与您的第一个应用程序屏幕之间尚存在一个短窗口,可以进行交互,在此期间,您可以显示一些品牌信息。这是实现启动屏幕的正确方法。
因此,以正确的方式实施“启动画面”时,您不需要单独的“启动画面片段”,因为这会导致App加载过程中不必要的延迟。为此,您只需要特殊的主题。从理论上讲,App主题可以应用于UI,并且比您的App UI初始化并变得可见要早得多。简而言之,您只需要SplashTheme这样:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_background</item>
</style>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
无论如何,您的片段将托管在一些活动中,我们称它为MainActivty。在Manifest刚刚添加SplashTheme到您的“活动”中(从用户单击“应用”图标开始,将显示启动屏幕主题):
<activity android:name=".ui.MainActivity"
android:theme="@style/SplashTheme">
Run Code Online (Sandbox Code Playgroud)
然后MainActivity返回到您的常规位置AppTheme,onCreate然后在super致电之前执行此操作:
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState)
.....
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4707 次 |
| 最近记录: |