簡體   English   中英

在片段的反壓下,使用導航圖阻止調用先前片段的onViewCreated

[英]On back press of fragment prevent to call onViewCreated of previous fragment using navigation graph

我正在使用jetpack導航來在片段之間導航。

我有兩個片段UserListFragmentProfileFragment 在UserListFragment的onViewCreated方法上調用服務器的API並單擊任何項​​目用戶將重定向到profileFragment。

如果用戶從ProfileFragment按回來,那么它應該來到正在運行的userListFragment。 onViewCreated方法再次調用,因此API再次調用從服務器獲取數據。 我不想調用第一個片段的onViewCreate 我想要與fragmentManager.add()相同的行為而不是fragmentManager.replace()。

這是我為導航編寫的代碼:

<fragment
    android:id="@+id/navigationUserListFragment"
    android:name="packageName.dashboard.UserListFragment"
    android:label="fragment_userlist"
    tools:layout="@layout/fragment_user_list">

   <action
        android:id="@+id/actionToProfile"
        app:destination="@id/navigationProfile"
        app:launchSingleTop="true"
        app:popUpTo="@id/navigationUserListFragment" />
</fragment>


<fragment
    android:id="@+id/navigationProfile"
    android:name="package.dashboard.ProfileFragment"
    android:label="profile_fragment"
    tools:layout="@layout/fragment_profile" />

所以我的問題是當用戶按下第二個片段時如何阻止調用onViewCreated

提前致謝

每當你從ProfileFragment再次來到UserListFragment時,OnViewCreated方法都會調用,因為無論何時打開新的配置文件片段,前一個片段的視圖都將被銷毀。

對於您的任務,我認為您應該使用LiveData機制或管理您的列表數據條件,如果它有值,那么API不應該調用。 是如果配置文件屏幕有任何更改,則通過通信進行接口。

暫無
暫無

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

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