4

I made a drawer for all activities ready by creating a BaseActivity:

public class BaseActivity extends AppCompatActivity implements 
    NavigationView.OnNavigationItemSelectedListener {

    private static final String SELECTED_ITEM_ID = "selected_id";
    public DrawerLayout mDrawerLayout;
    public Toolbar mToolBar;
    public NavigationView mDrawer;
    public ActionBarDrawerToggle mdrawerToggle;

    private int mSelectedId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null){
            mSelectedId = savedInstanceState.getInt(SELECTED_ITEM_ID);
        }
        Log.d("aoi", "save instance "+String.valueOf(mSelectedId));
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        Intent intent = null;
        if(mSelectedId != menuItem.getItemId()){
            mSelectedId = menuItem.getItemId();
            Log.d("aoi", "selected item "+String.valueOf(mSelectedId));

            mDrawerLayout.closeDrawer(GravityCompat.START);
            switch (menuItem.getItemId()){
                case R.id.agency_menu_item:
                    intent = new Intent(this, AgencyActivity.class);
                    startActivity(intent);
                    break;
            }
        }
        return false;
    }

     public void initDrawer(){
        mToolBar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(mToolBar);
        mDrawer = (NavigationView) findViewById(R.id.main_drawer);
        mDrawer.setNavigationItemSelectedListener(this);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_parent);
        mdrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                mToolBar,
                R.string.drawer_open,
                R.string.drawer_close);
        mDrawerLayout.setDrawerListener(mdrawerToggle);
        // indicator based on whether the drawerlayout is in open or closed
        mdrawerToggle.syncState();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SELECTED_ITEM_ID, mSelectedId);
    }
}

and will be extended by my Activities

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

 public class AgencyActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agency);
        initDrawer();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_agency, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

But then my problem is on how to disable the item in the drawer if it's showing already the activity? for example: I click the "agency" item on the drawer and when I'm already on that activity I won't be able to click it again, to avoid opening a new activity of AgencyActivity.

I tried using the onSaveInstanceState to save the value of item selected and compare it when I try to select it again, but no luck. I think it goes back to zero when the new activity is called via intent.

I'm Currently using Android Design support library for this one.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
alvin valdez
  • 161
  • 3
  • 13

1 Answers1

3

Just disable the menu when clicking:

menu.getItem(menuItem.getItemId()).setEnabled(false);

Then enable it when necessary with setEnabled(true);.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Im sorry for this question, but where can I put it? inside onNavigationItemSelected? because when i tried it the "menu" in menu.getItem(menuItem.getItemId()).setEnabled(false); is in red. sorry for that noob question – alvin valdez Jun 29 '15 at 14:17
  • `getItem(int itemID)` receives the id, in this case you must put inside menu item id, you use getItem() in any part when you have access to a `menuItem` and get `.getItemId()` for example... `onNavigationItemSelected` – Jordi Castilla Jun 29 '15 at 14:23
  • am I going to make a new method for getItem(int itemID)? – alvin valdez Jun 29 '15 at 15:12
  • nope! this method is in the menu, you just say here which menu position disable... check the api http://developer.android.com/reference/android/view/Menu.html#getItem(int) – Jordi Castilla Jun 29 '15 at 15:17
  • still cant figure out man. can you give me the snippet? because what I did is this: @Override public boolean onPrepareOptionsMenu(Menu menu) { menu.getItem(mSelectedId).setEnabled(false); return super.onPrepareOptionsMenu(menu); } but still doesnt disable the Item in the drawer – alvin valdez Jun 29 '15 at 15:48
  • check this answer: http://stackoverflow.com/a/5441329/3850595, since android 3.0 you must execute `invalidateOptionsMenu()` to force `onPrepareOptionsMenu()` being called... then you will be able to change menu options.... – Jordi Castilla Jun 30 '15 at 09:14
  • 1
    I made it work but Im not satisfied with the transition of opening a new activity. I'll switch on using a fragment. https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer. Thanks BTW with all your help. – alvin valdez Jun 30 '15 at 14:11