How to set active item in the Action Bar drop-down navigation?

前端 未结 5 1120
清歌不尽
清歌不尽 2020-12-13 00:23

I\'m trying to fix the issue with restarting activity on orientation changes.

I have an ActionBar with drop-down list navigation and after every rotatio

5条回答
  •  佛祖请我去吃肉
    2020-12-13 00:46

    As of support library v7, you just need to save / restore the state of the ActionBar:

    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selectedNavItem";
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Restore the previously serialized current dropdown position.
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getSupportActionBar().setSelectedNavigationItem(
                    savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        // Serialize the current dropdown position.
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getSupportActionBar()
                .getSelectedNavigationIndex());
    }
    

提交回复
热议问题