android / sunflower

A gardening app illustrating Android development best practices with migrating a View-based app to Jetpack Compose.
https://d.android.com/jetpack
Apache License 2.0
17.66k stars 4.71k forks source link

HomeViewPagerFragment will re-execute onCreateView #569

Open HeQuanLi opened 4 years ago

HeQuanLi commented 4 years ago

When I hit the back button on the PlantDetailFragment page to return to HomeViewPagerFragment, the HomeViewPagerFragment will re-execute onCreateView.What should I do if I don't want to.thank you

Zhuinden commented 4 years ago

Well replace.addToBackStack kills the previous view, and the only variant that doesn't (FragmentTransaction.show/hide) will eventually be deprecated because the Navigation team doesn't like it.

It is also impossible via setMaxLifecycle to make a Fragment stopped, but still keep its views alive.

OCNYang commented 3 years ago

When I hit the back button on the PlantDetailFragment page to return to HomeViewPagerFragment, the HomeViewPagerFragment will re-execute onCreateView. Why can the state of viewPager be recorded in the demo? It's exactly the same as it was when you entered the PlantDetailFragment. the HomeViewPagerFragment will re-execute onCreateView.Shouldn't it be the initial display state?

I was confused and trying to find the answer. Can you give me some help?

Zhuinden commented 3 years ago

@OCNYang as you navigate forward, replace().addToBackStack() will kill the Fragment view, but it won't kill the Fragment. The child fragments within a ViewPager have their views destroyed along with the parent view, but the child fragments themselves are kept alive via the child fragment manager.

Not entirely sure what behavior you were looking for.