I will answer into two parts -
Solution:
Do not inflate view every time you are coming back to previous fragment. Save View in a local variable and inflate it only once. Suggested by Ian Lake here
private var savedViewInstance: View? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return if (savedViewInstance != null) {
savedViewInstance
} else {
savedViewInstance =
inflater.inflate(R.layout.fragment_professional_details, container, false)
savedViewInstance
}
}
Explanation
The behavior you are getting is THE DEFAULT behavior, fragment will recreate their view each time you call navigator.popBackStack()
or use back button on device.
Let's understand life cycle of a fragment under navigation architecture.
Scenario: We are taking two fragments, HomeFragment and DashboardFragment. Both fragments belong to same NavGraph and start destination is Home Fragment.
Fragment Life Cycle on launching of app-
HomeFragment: onAttach:
HomeFragment: onCreate:
HomeFragment: onCreateView:
HomeFragment: onViewCreated:
HomeFragment: onActivityCreated:
HomeFragment: onStart:
HomeFragment: onResume:
On Navigation: Home Fragment ---> Dashboard Fragment
DashboardFragment: onAttach:
DashboardFragment: onCreate:
DashboardFragment: onCreateView:
DashboardFragment: onViewCreated:
DashboardFragment: onActivityCreated:
DashboardFragment: onStart:
DashboardFragment: onResume:
HomeFragment: onPause:
HomeFragment: onStop:
HomeFragment: onDestroyView:
On Navigation: Dashboard Fragment ---> Home Fragment
HomeFragment: onAttach:
HomeFragment: onCreate:
HomeFragment: onCreateView:
HomeFragment: onViewCreated:
HomeFragment: onActivityCreated:
HomeFragment: onStart:
HomeFragment: onResume:
DashboardFragment: onPause:
DashboardFragment: onStop:
HomeFragment: onDestroy:
DashboardFragment: onDestroyView:
DashboardFragment: onDestroy:
If we are saving view on intial HomeFragment: onCreateView() and inflating same view every time for next call of HomeFragment: onCreateView(), we can get old view restored.
If you notice HomeFragment: onDestroy()
will be called but after HomeFragment: onViewCreated()
has called. Calling of HomeFragment: onDestroy()
is just destroying old instance of HomeFragment.
I still believe this way of doing things are not best practice but it will be until, Google will come up something like onFragemntRestore()
.
On Another hand Fragments are suppose to be recreated every time they are removed or replaced and you are suppose to restore there states using onSaveInstanceState()
.
Here come the ViewModel to save the hustle of saving fragment state and restoring them.
To actually update the view, you must need to ViewModel and observe the changes to change in the views.
In simple words, If you have something
which is taking care of data for your views no matter where you are, if you back on same position without any changes, that something
has the information about how you were looking earlier. That something
is ViewModel
.
There are many other worth reading on same topic like this, this and this
Happy Coding !