Android onBackPressed() is not being called?

Steve2955 picture Steve2955 · Sep 16, 2016 · Viewed 22.2k times · Source

in my MainActivity, which extends from AppCompatActivity, I want to override the onBackPressed method like so:

@Override
public void onBackPressed() {
    Log.d("MainActivity","onBackPressed");
    Toast.makeText(getApplicationContext(),"onBackPressed",Toast.LENGTH_SHORT).show();
}

but onBackPressed does not get called. How ever if I do not override onBackPressed, the application closes, when I press the backbutton and if I do override it it doesn't.

The rest of my activity looks like this:

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private Drawer drawer;
private FloatingActionButton fab_test;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    fab_test = (FloatingActionButton) findViewById(R.id.fab_test);
    fab_test.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(),"FAB Test pressed",Toast.LENGTH_SHORT).show();
        }
    });

    buildDrawer();

    getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer,page).commit();
}

@Override
public void onBackPressed() {
    Log.d("MainActivity","onBackPressed");
    Toast.makeText(getApplicationContext(),"onBackPressed",Toast.LENGTH_SHORT).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);      
    return true;
}
}

EDIT: I'm talking about the hardware-backbutton(not the actionbar one)

Answer

Opiatefuchs picture Opiatefuchs · Sep 17, 2016

This question is already answered, but I feel to clear something here in this topic. Most comments and answeres point out to use super.onBackPressed() and that this is the cause of the not working method onBackPressed(). But that is not correct and important to let other beginners know. The method onBackPressed() does not need to use super.onBackPressed() . onBackPressed()also works if somebody, for example, comment super.onBackPressed() out.

As the questionier has written, he won´t use super.onBackPressed() because it will close the activity. So, the cause of this why it isn´t working, could be seperated into three possible causes:

  1. The Log doesn´t work because of a wrong filter in the logcat console
  2. The Toast dosn´t work because of the wrong passed context
  3. The OS is implemented wrong by the supplier.

Usually, the toast works by passing the correct context. In the case of questioner, simply passing this .

@Override
public void onBackPressed() {
    Log.d("MainActivity","onBackPressed");
    Toast.makeText(this,"onBackPressed",Toast.LENGTH_SHORT).show();
}

For the Log, simply set the correct filter on logcat.

I don´t care if somebody give downvotes now, but it must be clear for other beginners, that super.onBackPressed() must not be used.

Anyway, the use of onKeyDown() also is a solution.