android开发过程中的小问题——Jetpack的Navigation管理fragment栈
Jetpack 的 Navigation 管理 fragment 栈由于自己的项目使用的是 Jecpack 的 Navigation 来管理自己的 fragment,在使用过程中发现很多的退栈管理问题,于是乎在博客和简书上查阅资料,发现一篇不错的文章,分享出来Google控件Navigation进阶使用其中管理 fragment 栈有两个参数很重要popUpTo 和 popUpToIncl...
·
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 则不会弹出
然后说说我遇到的两种比较常见的退栈管理问题
-
三个循环的 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"/>
-
三个连续的 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"/>
就是简单的栈问题,了解到这两个参数的功能,就能理解这两种情况时怎么发生的。
更多推荐
已为社区贡献1条内容
所有评论(0)