How can I avoid clicking the same item in the drawer?

I made a box for all activities ready to create 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 expanded by my actions

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 how to disable the item in the drawer if it shows activity? for example: I click on the "agency" item in the inbox, and when I am already in this activity, I cannot click it again so as not to open a new activity AgencyActivity

.

I tried to use onSaveInstanceState

to keep the selected value and compare it when I try to select it again, but no luck. I think this falls back to zero when a new activity is called via intent.

I am currently using Android Design support for this.

+3


source to share


1 answer


Just disable the menu when clicked:

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

      



Then turn it on if necessary with setEnabled(true);

.

+2


source







All Articles