• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps onBackPressed doesn't work at all

ely_en

Lurker
Jul 1, 2013
4
0
I have a problem with these functions. I want to override the native back button, but the functions never get called and I don't understand where is the mistake.
Code:
@Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
            mCountDownTimer.cancel();
            Intent in = new Intent(getApplicationContext(), MyActivity2.class);
            startActivity(in);
            mCountDownTimer.cancel();
            return true;
        }else{
            return super.onKeyDown(keyCode, event);
        }
    }


    @Override
    public void onBackPressed(){
        mCountDownTimer.cancel();
        Intent in = new Intent(this, MyActivity2.class);
        startActivity(in);

        super.onBackPressed();

    }


Thank you!
 
Are you sure it doesn't work? When there was no super.onBackPressed(), what happened? Did the activity still destroy itself or nothing happened at all?

I'm asking because maybe it's starting the new activity part of the code which doesn't work, not onBackPressed()

Another thing is, I had no issue with onBackPressed but I didn't use onKeyDown along with it. I actually don't know, is KeyDown even needed? I'm not an Android expert myself D:
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones