android java onBackPressed deprecated

231 Views Asked by At

Why every SDK update it is more and more complicated ?

Who can find simplest to replace:

public void onBackPressed() {
 // Do stuff
 super.onBackPressed();
}

by:

OnBackPressedCallback callback = new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                // Do stuff

                // The next 3 lines replace: super.onBackPressed();
                setEnabled(false);
                getOnBackPressedDispatcher().onBackPressed();
                setEnabled(true);
            }
        };
        getOnBackPressedDispatcher().addCallback(this, callback);
    }

I'm very upset about all deprecated things and additional security that remove feature like launching your app on url pattern (you must own the site to make it work!)

1

There are 1 best solutions below

1
Phyo Thiha Win On

As for me, all I just do when I need to make onBackPress...

onBackPressedDispatcher.onBackPressed()