Android Navigation Components, How to know if result is set onViewCreated

Viewed 2757

In a fragment I setFragmentResultListener and wait for the results:

setFragmentResultListener(EnableFingerPrintFragment.ARG_REQUEST_KEY) { _, bundle ->
    bundle.getString(EnableFingerPrintFragment.ARG_RESULT_KEY)
        ?.let { isActivatedResult: String ->
            // DO something
        }
}
findNavController().navigate(R.id.to_enableFingerPrintFragment,)

When the result comes back this fragment gets Recreated and onCreateView is called.

How to know if the onCreateView is called for the first time fragment created OR it is from result coming back (after result is set in destination fragment)?

How to know if result is set onViewCreated to check this condition?

2 Answers

Probably you should use your viewmodel to retrieve data when your view have been recreated. For example, when you pop up a view from backstack your previous view is recreated but its viewmodel and data inside is not, so think about it.

Related