导航组件防止在按下返回键时重新创建片段[英] Navigation Component prevent to recreate fragment on back press

本文是小编为大家收集整理的关于导航组件防止在按下返回键时重新创建片段的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用项目中的JetPack导航组件和一些片段.

我有一个片段,上面有一个从服务器端填充的列表.然后,我在onViewCreated方法上调用getDataFromServer,然后,当用户单击项目时,新片段显示.

问题是,当我按下后按钮时,onViewCreated在我的列表片段中再次调用.

那么,如何防止我的第一个片段再次重现?我不需要不必要的onViewCreated电话.

推荐答案

当然,我们无法阻止呼叫oncrateView,但是有一种简单的方法. 我们可以在初始化viewModel

时调用view.loaddata()或其他生命周期方法

本文帮助我更好地了解ViewModel 5使用建筑组件

更新:

当前的导航组件(v 2.3.0)不支持此功能,它总是在导航到另一个片段时杀死片段.想象一下,您在片段中有Google地图,因此每次您再次返回它再次初始化的片段,相机移至用户位置! (多么坏主意).

因此,最好的方法是如果您有相同的问题,请勿使用导航组件.

导航,保存片段状态,github问题

更新2:

在某些情况下,例如过滤器或分页,我们可以使用诸如viewModel中的switchmap之类的转换,而不是在init函数中获取数据.

更新3:

如果您必须调用函数来从源加载数据,则有很多方法可以防止再次调用该函数,最简单的方法是在视图中调用getData(),请执行此操作.功能.第二个是使用懒惰变量,另一个是在Livedata上使用SwitchMap.有关更多信息,您可以找到所有解决方案在这里

其他推荐答案

也许您已经激活了图.

app:popUpTo="@+id/nav_fingerprint_capture"
app:popUpToInclusive="true"

其他推荐答案

当按下返回按钮时,您无法避免调用viewCreated方法或片段的任何方法,因此您应该更好地将视图模型与列表片段一起使用,并在视图模型中从服务器中获取数据.避免从片段中获取服务器中的数据,因为您已经使用了导航UI.

本文地址:https://www.itbaoku.cn/post/2091037.html

问题描述

I'm using the Jetpack Navigation Component in my project with a single activity and some fragments.

I have a fragment with a list that fills from server side. I call getDataFromServer on the onViewCreated method then, when a user clicks on an item, a new fragment shows.

The problem is that when I press the back button, onViewCreated is called again in my list fragment.

So, how can I prevent my first fragment from recreating again? I don't want unnecessary onViewCreated calls.

推荐答案

Of course, we can not prevent calling oncrateView, but there is a simple way. Instead of calling view.loadData() in onCreateView or other lifecycle methods we can call it while initializing ViewModel

this article helped me to know ViewModel better 5 common mistakes when using Architecture Components

Update:

The current navigation component (V 2.3.0) doesn't support this feature, it always kills the fragment while navigating to another fragment. Imagine you have google map in Fragment A so each time you returns to the Fragment it initialized again and the camera moves to the user location!! (what a bad idea).

So the best way is not to use the navigation component if you have the same issue.

Navigation, Saving fragment state, GitHub issue

Update 2:

In some cases like Filters or pagination, we can use Transformations like switchMap in our ViewModel instead of getting data in init function.

Update 3:

If you have to call a function to load data from a source, there are lots of ways to prevent call that function again, the first and easiest way is instead of calling getData() in your view, do it your ViewModel init function. the second one is using lazy variables and another one is using SwitchMap on livedata. for more information you can find all solutions here

其他推荐答案

Maybe you have activate the graph.

app:popUpTo="@+id/nav_fingerprint_capture"
app:popUpToInclusive="true"

其他推荐答案

You can't prevent calling onViewCreated method or any method of your fragment when back button pressed so you should better use view model with your list fragment and get data from server in your view model. Avoid getting data from server in your fragment since you already using Navigation UI.