简体   繁体   中英

How to avoid fragment recreation when tap back button using navigation architecture actions?

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.

I want to avoid that.

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.

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 ??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM