簡體   English   中英

在導航架構組件中手動將片段添加到后台堆棧中

[英]Adding fragment manually into the backstack in Navigation Architecture Component

我在導航圖中有 4 個片段,分別命名為A、B、C、D ,我從用戶那里獲取了一些數據。

當我處於正常流程(A->B->C->D)並按下返回按鈕時,一切(返回堆棧)工作正常。

但是我有一個我擁有並編輯信息流的情況,假設用戶將表單留在片段 D 上,所以當用戶再次出現在屏幕上時,我必須在同一屏幕上導航用戶,即片段 D。我通過 Deep 實現了它-關聯。

現在的問題是在上述情況下,除了片段 D 之外,堆棧中將沒有片段。但是我需要當用戶按下后退按鈕時,它應該導航到 Fragment C 到 Fragment B 到 Fragment A

意味着我希望將其他 Fragments(A,B,C) 添加到后台堆棧中。 我們可以做同樣的事情嗎,如果可以,怎么做?

===這里是代碼===

my_navigation_graph.xml。

<fragment
    android:id="@+id/aFragment"
    android:name="in.demo.project.AFragment"
    android:label="AFragment"
    tools:layout="@layout/fragment_a_layout">
    <deepLink
        android:id="@+id/deepLink"
        android:autoVerify="true"
        app:uri="demo://editflow?is_for_edit={is_for_edit}&amp;step={step}" />
    <argument
        android:name="is_for_edit"
        android:defaultValue="false"
        app:argType="boolean" />
    <argument
        android:name="step"
        android:defaultValue="2"
        app:argType="string" />

    <action
        android:id="@+id/action_aFragment_to_bFragment"
        app:destination="@id/bFragment"/>

    <action
        android:id="@+id/action_aFragment_to_cFragment"
        app:destination="@id/cFragment"/>
    <action
        android:id="@+id/action_aFragment_to_dFragment"
        app:destination="@id/dFragment"/>


<fragment
    android:id="@+id/bFragment"
    android:name="in.demo.project.BFragment"
    android:label="BFragment"
    tools:layout="@layout/fragment_b_layout">
    <action
        android:id="@+id/action_bFragment_to_cFragment"
        app:destination="@id/cFragment" />
</fragment>

<fragment
    android:id="@+id/cFragment"
    android:name="in.demo.project.CFragment"
    android:label="CFragment"
    tools:layout="@layout/fragment_c_layout">

    <action
        android:id="@+id/action_cFragment_to_dFragment"
        app:destination="@id/dFragment" />

</fragment>
<fragment
    android:id="@+id/dFragment"
    android:name="in.demo.project.DFragment"
    android:label="DFragment"
    tools:layout="@layout/fragment_d_layout">
    
</fragment>

導航通過。

findNavController().navigate(AFragmentDirections.actionAFragmentToBFragment())

在 AFragment 內部,我正在重定向到其他片段,假設為每個片段創建深層鏈接不是好的做法。

這段代碼在onCreate()方法的 AFragment

arguments?.let {
            if (AFragmentArgs.fromBundle(it).isForEdit) {
                redirectionForEdit(AFragmentArgs.fromBundle(it).step)
            } 
        }

和一個 function 用於重定向

  private fun redirectionForEdit(step: String) {
        when (step) {
            "2" ->
                findNavController().navigate(AFragmentDirections.actionAFragmentToBFragment())
            "3" ->
                findNavController().navigate(AFragmentDirections.actionAFragmentToCFragment())
            "4" ->
                findNavController().navigate(AFragmentDirections.actionAFragmentToDFragment())  

        }

    }

現在,如果我在步驟中得到 4,它將重定向到 DFragment,因此堆棧將為 A->D,但我也希望 B 和 D 進入堆棧(A->B->C->D)。

為此,請參閱在導航組件下處理自定義后退導航

OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
        @Override
        public void handleOnBackPressed() {
            // Handle the back button event
        }
    };
    requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM