0

I'm declaring a variable in the scope of my fragment class (LocalDate?):

class HomeFragment : Fragment() {

private var _binding: FragmentMainBinding? = null

// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!

private val homeViewModel: HomeViewModel by viewModels()
private var selectedDate: LocalDate? = null

The code then assigns a selectedDate based on an onclick listener that I assign in onViewCreated()

The initial state is correctly that selectedDate == null. However, when I select a date and then change my device rotation, and/or navigate back and forth between fragments, the date remains selected. To me this is unexpected behavior - according to this documentation , "Variables" which are defined as "Local variables in the fragment" are destroyed on config change, process death, etc. Why is this variable staying populated?

I'm pretty new to Android dev so probably misunderstanding something.

0 Answers0