skip to Main Content

I created a Java project on Android Studio. Android target API level 34 but the problem is onbackpressed deprecated. you can see this problem

   @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

Image

2

Answers


  1. You need to use onBackPressedDispatcher.addCallback(new ...) in your onCreate().

    You can test its execution by displaying a toast in the callback for example.

    Login or Signup to reply.
  2. Use this in your onCreate()

    OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                // handle backpressed here
            }
        };
        getOnBackPressedDispatcher().addCallback(this,onBackPressedCallback);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search