我目前在我的项目中使用 Android 导航架构。它有一个功能,可以使用快捷方式启动任何片段。目前我正在使用 NavController 在单击快捷方式时导航到所需的目的地。但是当我多次单击快捷方式时,每次都会创建一个新的片段实例。所以,我的问题是,有没有办法在使用 NavController 导航到片段时只接受一个片段实例?我在谷歌上搜索了很多次,但一无所获。提前致谢。
我正在尝试使用 android 架构组件创建单个活动应用程序。我有一个片段 A,它有一些文本字段,当用户按下一个按钮时,我导航到片段 B,在该应用程序使用如下代码导航回 A 后,他上传并编辑一些图像:
findNavController().navigate(R.id.action_from_B_to_A, dataBundle)
当导航回 B 时,使用dataBundle. 问题在于所有文本字段都重置了,因为片段 A 基本上是从头开始重新创建的。我在某处读到谷歌的开发人员建议您可以将视图保存在 var 中,而不是每次都膨胀它。所以尝试这样做:
private var savedViewInstance: View? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return if (savedViewInstance != null) {
savedViewInstance
} else {
savedViewInstance =
inflater.inflate(R.layout.fragment_professional_details, container, false)
savedViewInstance
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,所有文本字段在导航回 A 时都会重置。我做错了什么?处理此类案件的正确方法是什么?
这个问题已经被问过几次了,但我们现在是 2020 年,有没有人找到一个很好的可用解决方案呢?
我希望能够使用底部导航控件进行导航,而无需在每次选择片段时刷新它们。这是我目前所拥有的:
导航/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/main"
app:startDestination="@id/home">
<fragment
android:id="@+id/home"
android:name="com.org.ftech.fragment.HomeFragment"
android:label="@string/app_name"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/news"
android:name="com.org.ftech.fragment.NewsFragment"
android:label="News"
tools:layout="@layout/fragment_news"/>
<fragment
android:id="@+id/markets"
android:name="com.org.ftech.fragment.MarketsFragment"
android:label="Markets"
tools:layout="@layout/fragment_markets"/>
<fragment
android:id="@+id/explore"
android:name="com.org.ftech.ExploreFragment"
android:label="Explore"
tools:layout="@layout/fragment_explore"/>
</navigation>
Run Code Online (Sandbox Code Playgroud)
活动邮件.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<androidx.drawerlayout.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/main" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemIconTint="@color/nav"
app:itemTextColor="@color/nav"
app:layout_constraintBottom_toBottomOf="parent" …Run Code Online (Sandbox Code Playgroud) 我正面临着使用 Android 导航组件重新创建片段的问题。我已经集成了底部导航并将其与 android 导航组件结合起来。因此,每次单击底部栏上的选项卡时,都会重新创建片段并且不会保留旧状态。
即使我转到其他选项卡并返回,我也想保留片段的状态。我没有在任何地方找到任何解决方案。
除了底部导航,我使用navController.navigate()方法在不同的片段之间导航。
android android-fragments android-jetpack android-architecture-navigation android-jetpack-navigation