Hello,
adding OnBackPressedDispatcher
and OnBackPressedCallback
in my MainActivity cause onBackPressed
was deprecated.
old usage:
@Override
public void onBackPressed() {
//Display alert message when back button has been pressed
alertDialogShow();
}
New usage:
put it in my initialize listener call in MainActivity
onCreate()
- initListeners()
onBackPressedCallback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
//Display alert message when back button has been pressed
alertDialogShow();
}
};
onBackPressedDispatcher = new OnBackPressedDispatcher();
onBackPressedDispatcher.addCallback(this,onBackPressedCallback);
but the method alterDialogShow() is never called and the activity is terminated immediately. How can I solve this.
Thanks, Alejandro
You create a new OnBackPressedDispatcher
but you must use the Activity's OnBackPressedDispatcher
and add your callback to it:
//onBackPressedDispatcher = new OnBackPressedDispatcher();
onBackPressedDispatcher = getOnBackPressedDispatcher();
onBackPressedDispatcher.addCallback(this, onBackPressedCallback);