toolbar actions triggering settings menu

1k Views Asked by At

i have a toolbar with a typical settings activity attached to the 3 dot menu.

In one of my fragments i change the toolbar to add a couple of icons, but when these icons are pressed it runs its method and then launches the typical settings activity,toolbar icon settings

heres how i call my settings in my main activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem mItem) {
    // 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 = mItem.getItemId();
    Intent intent = new Intent(MainActivity.this, Settings.class);
    startActivity(intent);

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

    return super.onOptionsItemSelected(mItem);
}

and here is how i add items and use them in my fragment

@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
    // Inflate the menu items for use in the action bar

    inflater.inflate(R.menu.set_menu, menu);

    mShare = menu.findItem(R.id.share);
    mSave = menu.findItem(R.id.save);

    super.onCreateOptionsMenu(menu, inflater);

}

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.share:
             share();
            break;

        case R.id.save:
            saveWallpaper();

            return true;
        default:
    }
    return true;
}

im still kinda new to android and hoping this is rather trivial thanks for any and all help

1

There are 1 best solutions below

0
On BEST ANSWER

Your onOptionsItemSelected() unconditionally calls startActivity(), rather than only calling it when the Settings option is selected. Move those lines within the if statement:

@Override
public boolean onOptionsItemSelected(MenuItem mItem) {
    // 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 = mItem.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Intent intent = new Intent(MainActivity.this, Settings.class);
        startActivity(intent);
        return true;
    }

    return super.onOptionsItemSelected(mItem);
}