Alex Rivera | Logout

android action bar onNavigationItemSelected

Asked 2012-03-01T14:16:39.777
13

I'm developing for android 3+

In my action bar i have a drop-down list(see how to hide/unhide the actionbar list on android 3? for the dropdown i intend). The problem is i need to do a certain action when the user selects something, but Android calls onNavigationItemSelected() as soons as it draws the view, so no selection actually happened.

How can i detect if the user actually pressed something and it is not a fake call from android ?

public class ListDittaListener implements OnNavigationListener{

    private BaseActivity activity;

    private ListDittaListener()
    {

    }

    public ListDittaListener(BaseActivity activity)
    {
        this.activity = activity;
    }

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId)
    {
        MyApp appState = ((MyApp)this.activity.getApplicationContext());
        appState.setDittaSelezionata( (int) itemId);

        SharedPreferences settings = this.activity.getSharedPreferences(MyApp.PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("ditta_id_selezionata", (int) itemId);

            ////////restart activity this.activity.recreate();

        return false;
    }

}
Edit
Report

1 Answer

16

You can easily just ignore the first call to onNavigationItemSelected if you like:

public class Whatever implements OnNavigationListener {
    private boolean synthetic = true;

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        if (synthetic) {
            synthetic = false;
            return true;
        }

        // do whatever you really wanted here 
    }
}
answered 2012-03-14T06:03:15.803

Your Answer