当使用导航架构动作点击返回按钮时,如何避免片段重新生成? [英] How to avoid fragment recreation when tap back button using navigation architecture actions?

查看:100
本文介绍了当使用导航架构动作点击返回按钮时,如何避免片段重新生成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用android studio导航组件创建一些片段,并使用动作在它们之间切换.问题是,每次我单击后退"按钮从Fragment2返回Fragment1时,都会重新创建一个片段.

I am using android studio navigation component to create some fragments and switch between them using the actions. Problem is that each time I tap Back button to go back from a Fragment2 to Fragment1, Fragment one is recreated.

我想避免这种情况.

我找到了一种解决方案,当人们不使用导航组件时,可以简单地将".replace"替换为".add",但是找不到有关如何使用导航组件进行操作的信息.

I found solutions when one don't use navigation component and simply can replace ".replace" with ".add", but can't find any information on how to do the same with navigation component.

推荐答案

我认为重新创建片段1是正常的事情,我认为您有一个片段容器.从片段1移到片段2时,您可以使用FragmentTransaction.commit()将片段1替换为片段2布局. 如果要控制应用程序的后退按钮,则可以在Mainactivity类中重写Onbackpresed方法. 按下后退按钮??

I think it's a normal thing that your fragment 1 is recreated, I think you have a fragment container. And when you move from fragment 1 to fragment 2, you replace the fragment 1 with fragment 2 layout using FragmentTransaction.commit() If you want to control the back button of your App, you can override the Onbackpresed method in your Mainactivity class. what specifically you want to do when you press the back button ??

这篇关于当使用导航架构动作点击返回按钮时,如何避免片段重新生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆