[英]How can I send the array from the first fragment to the second fragment using Navigation Component?
[英]How can I back from the second fragment to the first fragment using Navigation Component?
如何使用導航組件從第二個片段返回到第一個片段?
導航_main.xml
<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_navigation"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="com.test.ttt.FirstFragment"
tools:layout="@layout/fragment_first">
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment"
app:popUpTo="@id/firstFragment"
app:popUpToInclusive="true"/>
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.test.ttt.SecondFragment"
tools:layout="@layout/fragment_second" />
</navigation>
FirstFragment.java
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Navigation.findNavController(container).navigate(FirstFragmentDirections.actionFirstFragmentToSecondFragment());
return inflater.inflate(R.layout.fragment_first, container, false);
}
}
我想當我按下第二個片段中的后退按鈕時,它必須返回到第一個片段,但目前,當我按下第二個片段中的后退按鈕時,它正在離開應用程序。
我怎么解決這個問題?
您退出應用程序的原因是因為在導航到第二個片段時,您正在從堆棧中刪除第一個片段。 屬性app:popUpTo
和app:popUpToInclusive
通常在您想要在導航時從后台刪除一些片段時使用。 一個簡單的用例是從 Login/SignUp Fragment 導航到 Home/Main Fragment 時。 在這種情況下,您不希望用戶能夠按返回和 go 返回登錄/注冊屏幕。 這是這兩個屬性可以提供幫助的地方。
TL;DR - 從您的操作中刪除app:popUpTo
和app:popUpToInclusive
屬性
改變
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment"
app:popUpTo="@id/firstFragment"
app:popUpToInclusive="true"/>
到
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.