Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android action bar onNavigationItemSelected

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;
    }

}
like image 224
max4ever Avatar asked Mar 01 '12 14:03

max4ever


People also ask

What is Android action bar?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.

What's action bar?

An on-screen toolbar displaying icons that are clicked or tapped to perform various functions. For example, the menu bar at the top of an Android app is called an action bar.


1 Answers

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 
    }
}
like image 68
mlc Avatar answered Sep 20 '22 18:09

mlc