代码之家  ›  专栏  ›  技术社区  ›  Ashish Joshi

未使用导航图弹出片段

  •  0
  • Ashish Joshi  · 技术社区  · 1 年前

    isUserRegistered 在中为true SharedPreferences

    //declared at the top in onCreateView() of fragment
    val prefs = appContext!!.getSharedPreferences("expense_prefs", Context.MODE_PRIVATE)
    

    val editor = prefs.edit()
    editor.putBoolean("isUserRegistered", true)
    editor.apply()
    findNavController().navigate(R.id.action_getStartedFragment_to_homeFragment)
    

    //in the same onCreateView() of fragment1
    val isUserRegistered = prefs.getBoolean("isUserRegistered", false)
    if (isUserRegistered)
        findNavController().navigate(R.id.action_getStartedFragment_to_homeFragment)
    

    下面是我的导航文件,其中我还添加了 app:popUpToInclusive="true" 在两种导航中。

    <?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/navigation"
        app:startDestination="@id/getStartedFragment">
        <fragment
            android:id="@+id/getStartedFragment"
            android:name="net.softglobe.expensemanager.GetStartedFragment"
            android:label="fragment_get_started"
            tools:layout="@layout/fragment_get_started" >
            <action
                android:id="@+id/action_getStartedFragment_to_homeFragment"
                app:destination="@id/homeFragment"
                app:popUpToInclusive="true" />
        </fragment>
        <fragment
            android:id="@+id/homeFragment"
            android:name="net.softglobe.expensemanager.HomeFragment"
            android:label="fragment_home"
            tools:layout="@layout/fragment_home" >
            <action
                android:id="@+id/action_homeFragment_to_getStartedFragment2"
                app:destination="@id/getStartedFragment"
                app:popUpToInclusive="true" />
        </fragment>
    </navigation>
    

    isUserRegistered 是真的。请帮助解决问题。如果没有解决方案,请至少向上投票以获得更广泛的观众。

    1 回复  |  直到 1 年前
        1
  •  0
  •   PineapplePie    1 年前

    看看 this 文章

    您还可以包括app:poputpinclusive=“true”,以指示 应用程序中指定的目的地:也应从 后堆叠。

    你没有提供所需的片段到你应该被弹出的地方,比如 app:popUpTo="@id/getStartedFragment"

    推荐文章