user3281737
user3281737

Reputation: 13

Android StartActivity option menu

I'm trying to start an activity from OptionsMenu, but it doesn't start. Why?

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    case R.id.main:
        Intent intent = null;
        intent = new Intent(MainActivity.this, Impostazioni.class);
        this.startActivity(intent);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }


}

Upvotes: 1

Views: 796

Answers (2)

Jorgesys
Jorgesys

Reputation: 126445

// Delay is in milliseconds
static final int DRAWER_DELAY = 200;

@Override
public boolean onOptionsItemSelected(MenuItem item) {

  //open navigationDrawer
  new Handler().postDelayed(openDrawerRunnable(), DRAWER_DELAY);

    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    case R.id.main:
        Intent intent = null;
        intent = new Intent(MainActivity.this, Impostazioni.class);
        this.startActivity(intent);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }

}

Method to open the Navigation Drawer...

private Runnable openDrawerRunnable() {
    return new Runnable() {

        @Override
        public void run() {
            mDrawerToggle.openDrawer(Gravity.LEFT);
        }
    }
}

Upvotes: 0

SDJMcHattie
SDJMcHattie

Reputation: 1699

Don't you end up returning early when you do?

if (mDrawerToggle.onOptionsItemSelected(item)) {
    return true;
}

You never reach the code for startActivity I think. If that's not the case, are you sure the menu item has id R.id.main? I would want to debug this code with a break point at the top of this method - then step through and see what gets called and what doesn't.

Upvotes: 2

Related Questions