Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnBackPressed is not being called?

Tags:

android

I have override the OnBackPressed function inside my activity, but it's not being called. On other activities it's working fine. Here is my method:

@Override
public void onBackPressed() {
    Log.e("back",""+1); 
    UserPage.getstate().finish();
    Intent i=new Intent(CreateGroup.this,UserPage.class);
    i.putExtra("title11","dd");
    startActivity(i);
    finish();
}

This method is not being called, and the default OnBackPresssed is called every time i press the back button.

like image 964
DoYou EvenLift Avatar asked May 20 '13 09:05

DoYou EvenLift


People also ask

When onBackPressed is Called android?

The onBackPressed() is a default action called from onKeyDown() in API < 5 and a default action called from onKeyUp() from API level 5 and up. If onKeyUp() does not call super. onKeyUp() , onBackPressed() will not be called.

What is activity in android studio?

An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.


3 Answers

Try this code

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Log.e("back",""+1); 
        UserPage.getstate().finish();
        Intent i=new Intent(CreateGroup.this,UserPage.class);
        i.putExtra("title11","dd");
        startActivity(i);
        finish();
    }
    return true;
}
like image 139
Jeff Lee Avatar answered Sep 28 '22 18:09

Jeff Lee


Had a similar issue. The problem for me was in my override of onKeyDown() hiding the KEYCODE_BACK keydown events.

like image 23
Doug Coburn Avatar answered Sep 28 '22 18:09

Doug Coburn


Have you tried this? ,

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back pressed.


        return true;
    }

    return super.onKeyDown(keyCode, event);
}
like image 31
Basim Sherif Avatar answered Sep 28 '22 20:09

Basim Sherif