【发布时间】:2019-06-14 19:38:12
【问题描述】:
我正在使用喷气背包导航在片段之间导航。
我有两个片段 UserListFragment 和 ProfileFragment。
在调用服务器 API 的 UserListFragment 的 onViewCreated 方法上,用户点击任何项目都会重定向到 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" />
所以我的问题是当用户按下第二个 Fragment 时,如何防止调用 onViewCreated。
提前致谢
【问题讨论】:
标签: android xml android-fragments navigation