Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is onDestroyView in a fragment called immediately after navigating with JetPack Navigation

Is it normal behaviour that onDestroyView() is called immediately when you navigate to a different fragment?

I call the following method to navigate:

findNavController().navigate(R.id.action_homefragment_to_detailsfragment)

Is there a way to avoid this? Or should everything be restored from a ViewModel? For example my last scroll position in a ScrollView..

like image 337
Luciano Avatar asked Nov 26 '18 15:11

Luciano


1 Answers

Fragments on the back stack have their views destroyed, but the view state is saved and automatically restored when it comes to the top of the stack again (i.e., you hit the system back button).

Only views with an android:id have their state saved and restored, so make sure any important views have an id.

like image 175
ianhanniballake Avatar answered Oct 13 '22 20:10

ianhanniballake