Jetpack 的 Navigation 管理 fragment 栈

由于自己的项目使用的是 Jecpack 的 Navigation 来管理自己的 fragment,在使用过程中发现很多的退栈管理问题,于是乎在博客和简书上查阅资料,发现一篇不错的文章,分享出来Google控件Navigation进阶使用

其中管理 fragment 栈有两个参数很重要

  • popUpTo 和 popUpToInclusive (使用在 nav_grap h的每个 fragment 的 action 里)
    • popUpTo(tag) 跳转到 tag,并弹出 tag 之上的 fragment
    • popUpToInclusive=true 会弹出 tag,false 则不会弹出

然后说说我遇到的两种比较常见的退栈管理问题

  1. 三个循环的 Fragment,A -> B -> C -> A , A 返回时不会再回退到 B 和 C,而是返回到之前到达 A 的前一个 Fragment

    <fragment
        android:id="@+id/FragmentA"
        android:name="com.aexample.forain.fragment.FragmentA"
        android:label="a"
        tools:layout="@layout/fragment_a">
        <action
            android:id="@+id/action_FragmentA_to_FragmentB"
            app:destination="@id/FragmentB"/>
    <fragment
        android:id="@+id/FragmentB"
        android:name="com.example.forain.fragment.FragmentB"
        android:label="b"
        tools:layout="@layout/fragment_b">
        <action
            android:id="@+id/action_FragmentB_to_FragmentC"
            app:destination="@id/FragmentC"/>
    <fragment
        android:id="@+id/FragmentC"
        android:name="com.example.forain.fragment.FragmentC"
        android:label="c"
        tools:layout="@layout/fragment_c">
        <action
            android:id="@+id/action_FragmentC_to_FragmentA"
            app:destination="@id/FragmentA"
            app:popUpTo="@id/FragmentA"
            app:popUpToInclusive="true"/>    
  2. 三个连续的 Fragment,A -> B -> C,C 返回时不会经过 B,而是直接回到 A

    <fragment
         android:id="@+id/FragmentA"
         android:name="com.aexample.forain.fragment.FragmentA"
         android:label="a"
         tools:layout="@layout/fragment_a">
         <action
                android:id="@+id/action_FragmentA_to_FragmentB"
                app:destination="@id/FragmentB"/>
    <fragment
         android:id="@+id/FragmentB"
         android:name="com.example.forain.fragment.FragmentB"
         android:label="b"
         tools:layout="@layout/fragment_b">
         <action
                android:id="@+id/action_FragmentB_to_FragmentC"
                app:destination="@id/FragmentC"
                app:popUpTo="@id/FragmentB"
         	    app:popUpToInclusive="true"/>	   

    或者

    <fragment
          android:id="@+id/FragmentA"
          android:name="com.aexample.forain.fragment.FragmentA"
          android:label="a"
          tools:layout="@layout/fragment_a">
          <action
                 android:id="@+id/action_FragmentA_to_FragmentB"
                 app:destination="@id/FragmentB"/>
     <fragment
          android:id="@+id/FragmentB"
          android:name="com.example.forain.fragment.FragmentB"
          android:label="b"
          tools:layout="@layout/fragment_b">
          <action
                 android:id="@+id/action_FragmentB_to_FragmentC"
                 app:destination="@id/FragmentC"
                 app:popUpTo="@id/FragmentA"
                 app:popUpToInclusive="false"/>    

就是简单的栈问题,了解到这两个参数的功能,就能理解这两种情况时怎么发生的。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐