0

I have a LiveData observer on LiveBarcodeScanningFragment that observes barcode live data, after which if detected, BarcodeBottomSheetFragment will start (extend Fragment instead of BottomSheetDialogFragment which have more animations).

But here's the problem, the observer on LiveBarcodeScanningFragment triggered immediately after loading the fragment (Have implement SingleLiveEvent extend MutableLiveData in Google).

I have test AlertDialog can achieve my goal. In Debug mode, I saw that, when I use AlertDialog, it will finish observation, then open AlertDialog. When I dismiss the dialog, It will not retrigger the observation.

Here are my codes

wm= ViewModelProvider(this@LiveBarcodeScanningFragment).get(WorkflowModel::class.java)


wm.searchedProduct.observe(viewLifecycleOwner) { searchedRes ->

        // THIS fragment = (LiveBarcodeScanningFragment)
        //  extend fragment instead of BottomSheetDialogFragment,
        //  it will open new fragment directly, block the observation,
        // and occur the problem, when I return to THIS fragment(LiveBarcodeScanningFragment),
        // it will run here again and again. (been observed to get changed )
        // BG: BarcodeBottomSheetFragment replace THIS fragment.
        /*
        *  private fun openFragment(
        *  fm: FragmentManager,
        *  frag: BarcodeBottomSheetFragment, fragTag: String
        *  ) {
        *  fm.beginTransaction()
        *      .replace(
        *          R.id.activity_live_barcode_container,
        *          frag
        *      )
        *      .addToBackStack(fragTag)
        *      .commit()
        *   }
        * */
        BarcodeBottomSheetFragment.openAddBarcodeToDatabaseAndCartPage(
            parentFragmentManager,
            getDeviceInfo(),
            null,
            wm.adapter!!,
            searchedRes
        )

        // use Debug, I saw, it will finish observation, then open AlertDialog.
        // will not re-trigger observation
        AlertDialog.Builder(requireContext())
            .setTitle("Delete entry")
            .setMessage("Are you sure you want to delete this entry?")
            .setPositiveButton(
                android.R.string.yes
            ) { dialog, which -> }
            .setNegativeButton(android.R.string.no, null)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show()
        Timber.d("AlertDialog END... ")
    }

        // will not re-trigger observation

update:

Here is my temporary solution, use add rather than replace like DialogFragment.show method do.

fm.beginTransaction() 
    .add(R.id.activity_live_barcode_container, frag)
    .addToBackStack(fragTag).commit() 
Jared DC
  • 35
  • 6
  • Here is my temporary solution, use add rather than replace fm.beginTransaction() .add(R.id.activity_live_barcode_container, frag).addToBackStack(fragTag).commit() like DialogFragment.show method do. – Jared DC Mar 19 '22 at 16:55

1 Answers1

1

But here's the problem, the observer on LiveBarcodeScanningFragment triggered immediately after loading the fragment

This is by design. LiveData delivers updates only when data changes, but also only to active observers. In this context, active refers to the lifecycle state of the app component. So, for example, if you have your observer registered in the onResume function of your fragment, it will receive an update when it resumes, triggering your AlertDialog.

You didn't share code with your observer call, but you'll want to make sure you move it to onCreate if not there already. This section in Observe LiveData objects explains it the best.

zen_of_kermit
  • 1,404
  • 2
  • 13
  • 19
  • Can you pleae explain your claim? `If you have your observer registered in the onResume function of your fragment, it will receive an update when it resumes`. Why so? It makes zero logical sense. Observers are to observe CHANGES in data, not resuming fragment lol. Is there a part of docs where it is explained thoroughly why it happens, or Googlers screwed up something AGAIN? :) – qkx Jun 10 '22 at 10:20
  • It's not a *claim*, it's literally explained how and why it works that way in the documentation which I linked, which clearly neither you nor the OP have read. – zen_of_kermit Jun 11 '22 at 00:22
  • I checked your post on mobile so I missed the link. Now that I read it, you are right - kinda weird decision though, but Google thought otherway...Thx – qkx Jun 11 '22 at 04:38