HomeAsUp Button has no effect in Android 4.2.2 wit

2020-02-29 21:46发布

问题:

I'm using Toolbar from Appcompat 21 instead of standard actionbar, everything goes smoothly except that the HomeAsUp Button in an extra activity has no effect.

When I open the PrefsActivity (code snippets below), the HomeAsUp button shows normally (a small left arrow), When I click the button, from the code, I want it "finish", but it just remains no change. When I add the onCreateOptionsMenu function to BaseActivity and inflate the global.xml which only has one menu entry Settings, the click acts as expected, but an extra action menu overflow including the Settings appears, which I don't want. Then I tried to edit the global.xml, deleting the default Settings menu entry, the results is that the button gives no change again.

When testing on an 4.4.2 device with the same code, the problem disappeared.

My question is, how to make the HomeAsUp button of the PrefsActivity effect while not adding the menu entry by onCreateOptionsMenu in BaseActivity?

Some code segments is as following:

BaseActivity:

public abstract class BaseActivity extends ActionBarActivity {
    protected Toolbar mActionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResource());
        mActionBar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(mActionBar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    protected abstract int getLayoutResource();

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
            case R.id.action_settings:
                startActivity(new Intent(this, PrefsActivity.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

     // add this function, the button acts normally, but an extra 
     // action overflow menu   appears in the right side of the Toolbar  
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.global, menu);
        return true;
   }

   //...
}

PrefsActivity.java:

public class PrefsActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction()
            .replace(R.id.frag_container, new PrefsFragment())
            .commit();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_prefs;
    }
}

回答1:

I've done some similar research into the issue that I would like to contribute here. I can confirm that with without any items in the menu, the home/up event is not fired in Android 4.2.2 when using the support Toolbar.

The same issue is present in the latest version of the support library, 21.0.2.
manifest settings of parentactivity and meta parentactivity have no effect.
manipulating settings via getSupportActionBar().setXXX() have no effect.
The only "workaround" I can think of is to use toolbar.setNavigationOnClickListener() to get the event.

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(ToolbarActivity.this, "Up clicked", 
                Toast.LENGTH_SHORT).show();
            NavUtils.navigateUpFromSameTask(ToolbarActivity.this);
        }
    });
}

This way at least other platforms can function properly, and v17 can have this weird workaround so the toolbar isn't completely broken on jellybean mr1.

I've filed this as a bug with Google which can be tracked here: https://code.google.com/p/android/issues/detail?id=81528



回答2:

Inside your PrefsActivity add:

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

Also see the docs for a discription of these functions and other functions to serve your purpose.