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?