【发布时间】:2019-01-26 20:57:18
【问题描述】:
根据navigation priciples,您的应用中的第一个目标应该是您的用户在注册/登录或任何其他条件导航后启动应用时通常会看到的屏幕,我将该起始目标称为“homeFragment”。
遵循此原则并阅读conditional navigation by Maria Neumayer 上的帖子后,我在通过条件导航流程时遇到了工具栏和后退导航的一些问题。
我正在使用具有 ConstraintLayout、Toolbar 和 NavHostFragment 的单个 Activity 来构建应用程序:
<android.support.constraint.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activities.NavigationTestActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
/>
</android.support.constraint.ConstraintLayout>
主图如下所示,起始目的地为起始目的地,连接到细节片段的操作(此操作由按钮触发)和使用嵌套图实现的条件导航:
我将此嵌套图称为welcomeGraph,它包括登录或注册屏幕,您可以在此处查看:
在 homeFragment onResume 中,我检查登录/注册是否已完成(由存储在 sharedPrefs 中的虚拟布尔值确定),如果没有,我启动注册/登录的欢迎嵌套图。
在登录目标中,我有一个“已完成”按钮,它将 sharedPrefs 中的虚拟布尔值设置为 true,并触发一个动作 popToWelcomeGraph(包括),它应该关闭整个嵌套图并将我带回 homeFragment(这可行)。
问题 - 嵌套图中的工具栏问题:
由于欢迎图在用户登陆应用程序后立即启动,因此工具栏不应在该嵌套图的第一个目的地显示后退/向上箭头,而是应该感觉好像它是应用程序的第一个屏幕,然后点击返回应该退出应用程序。
问题:是否可以在此处更改工具栏以模拟嵌套图表中的第一个屏幕是应用程序中的第一个屏幕,直到登录/注册完成?这是一种不好的做法吗?
【问题讨论】:
标签: android android-architecture-navigation