Joao Sardinha
Joao Sardinha

Reputation: 3423

back button not responding properly

This is really weird and hard to explain. I override onOptionsItemSelected in my activity, but it only starts functioning after a certain fragment has started, I really don't get it...

Activity

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    Log.e("hm", ""+ item.getItemId());

    switch (item.getItemId()) {

        case android.R.id.home:
            Toast.makeText(Activity_Main.this, "back", Toast.LENGTH_SHORT).show();
            onBackPressed();
            return true;

        case R.id.settings_toolbar:
            Toast.makeText(Activity_Main.this, "setings", Toast.LENGTH_SHORT).show();
            toSettings();
            return true;

        case R.id.share_toolbar:
            Toast.makeText(getApplicationContext(), "shared lmao", Toast.LENGTH_SHORT).show();
            return true;

    }
    return super.onOptionsItemSelected(item);
}

Starting fragment that doesn't respond to toolbar actions:

public void toSettings() {

    Fragment_Settings frag = new Fragment_Settings();
    fm.beginTransaction()
            .replace(R.id.mainContainer, frag)
            .addToBackStack(null)
            .commit();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.arrow_left);
}

Please someone help.

Upvotes: 0

Views: 43

Answers (1)

Joao Sardinha
Joao Sardinha

Reputation: 3423

For all the unforgiven souls who are having this haunting experience, for some reason, you have to inflate a menu in onCreateOptionsMenu() in order for the back button to work.

Upvotes: 2

Related Questions