Fixed fragment back

This commit is contained in:
M66B 2022-06-15 22:20:02 +02:00
parent b8b4a9c685
commit 03beb691e8
1 changed files with 4 additions and 4 deletions

View File

@ -422,17 +422,17 @@ public class FragmentBase extends Fragment {
}
protected void setBackPressedCallback(OnBackPressedCallback backPressedCallback) {
backPressedCallback.setEnabled(false);
FragmentActivity activity = getActivity();
if (activity == null)
return;
activity.getOnBackPressedDispatcher().addCallback(backPressedCallback);
backPressedCallback.setEnabled(true);
getViewLifecycleOwner().getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny() {
Lifecycle.State state = getViewLifecycleOwner().getLifecycle().getCurrentState();
backPressedCallback.setEnabled(state.isAtLeast(Lifecycle.State.STARTED));
if (state.isAtLeast(Lifecycle.State.DESTROYED))
if (state.isAtLeast(Lifecycle.State.STARTED))
activity.getOnBackPressedDispatcher().addCallback(backPressedCallback);
else
backPressedCallback.remove();
}
});