Code example for ActionBar

Methods: getDisplayOptions, getNavigationMode, setNavigationMode, setSelectedNavigationItem, setTitle

0
        // All the flags we may change in this method. 
        final int MASK = ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME
                | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM;
 
        // The current flags set to the action bar.  (only the ones that we may change here) 
        final int current = mActionBar.getDisplayOptions() & MASK;
 
        // Build the new flags... 
        int newFlags = 0;
        newFlags |= ActionBar.DISPLAY_SHOW_TITLE;
        if (mShowHomeIcon) {
            newFlags |= ActionBar.DISPLAY_SHOW_HOME;
        } 
        if (mSearchMode) {
            newFlags |= ActionBar.DISPLAY_SHOW_HOME;
            newFlags |= ActionBar.DISPLAY_HOME_AS_UP;
            newFlags |= ActionBar.DISPLAY_SHOW_CUSTOM;
        } 
        mActionBar.setHomeButtonEnabled(mSearchMode);
 
        if (current != newFlags) {
            // Pass the mask here to preserve other flags that we're not interested here. 
            mActionBar.setDisplayOptions(newFlags, MASK);
        } 
    } 
 
    private void update() { 
        boolean isIconifiedChanging = mSearchView.isIconified() == mSearchMode;
        if (mSearchMode) {
            setFocusOnSearchView(); 
            // Since we have the {@link SearchView} in a custom action bar, we must manually handle 
            // expanding the {@link SearchView} when a search is initiated. Note that a side effect 
            // of this method is that the {@link SearchView} query text is set to empty string. 
            if (isIconifiedChanging) {
                final CharSequence queryText = mSearchView.getQuery();
                mSearchView.onActionViewExpanded();
                if (!TextUtils.isEmpty(queryText)) {
                    mSearchView.setQuery(queryText, false);
                } 
            } 
            if (mActionBar.getNavigationMode() != ActionBar.NAVIGATION_MODE_STANDARD) {
                mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            } 
            if (mListener != null) {
                mListener.onAction(Action.START_SEARCH_MODE);
            } 
        } else { 
            final int currentNavigationMode = mActionBar.getNavigationMode();
            if (mActionBarNavigationMode == ActionBar.NAVIGATION_MODE_TABS
                    && currentNavigationMode != ActionBar.NAVIGATION_MODE_TABS) {
                // setNavigationMode will trigger onTabSelected() with the tab which was previously 
                // selected. 
                // The issue is that when we're first switching to the tab navigation mode after 
                // screen orientation changes, onTabSelected() will get called with the first tab 
                // (i.e. favorite), which would results in mCurrentTab getting set to FAVORITES and 
                // we'd lose restored tab. 
                // So let's just disable the callback here temporarily.  We'll notify the listener 
                // after this anyway. 
                mTabListener.mIgnoreTabSelected = true;
                mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                mActionBar.setSelectedNavigationItem(mCurrentTab);
                mTabListener.mIgnoreTabSelected = false;
            } else if (mActionBarNavigationMode == ActionBar.NAVIGATION_MODE_LIST
                    && currentNavigationMode != ActionBar.NAVIGATION_MODE_LIST) {
                mNavigationListener.mIgnoreNavigationItemSelected = true;
                mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
                mActionBar.setSelectedNavigationItem(
                        getNavigationItemPositionFromTabPosition(mCurrentTab));
                mNavigationListener.mIgnoreNavigationItemSelected = false;
            } 
            mActionBar.setTitle(null);
            // Since we have the {@link SearchView} in a custom action bar, we must manually handle 
            // collapsing the {@link SearchView} when search mode is exited. 
            if (isIconifiedChanging) {
                mSearchView.onActionViewCollapsed();
            }