1

I have a project which contains one activity and one fragment. The activity observes a LiveData variable, and when the variable is a certain value it shows a fragment.

// Activity onCreate
mViewModel.getConnectionState().observe(this, state -> {
  if (state == 3) { // example
    mMyFragment.show(getSupportFragmentManager(), TAG);
  }
});

However, I notice that when the fragment is active, and I rotate the screen, the fragment is recreated, and also the LiveData gets triggered again due to the activity being recreated, which launches the fragment a second time. what's the best way to handle this so that the fragment is only recreated once?

Jeff L
  • 491
  • 5
  • 14
  • You can use `SingleLiveEvent`. Check [this answer](https://stackoverflow.com/questions/65266324/how-to-use-single-live-event-to-show-toast-in-kotlin). – ysfcyln Dec 01 '21 at 06:22

0 Answers0