0

I implemented a BaseActivity which extends the ActionBarActivity and implements a NavigationDrawer. All my Activities are inheriting from this BaseActivity. Im now wondering if i can implement a NavigationDrawer and still have the proper up navigation using the toolbar or is the device back button supposed to work as the up navigation button when i implemented a NavigationDrawer?

EDIT:

Custom NavigationDrawer with Toolbar

public abstract class NavigationActivity extends ActionBarActivity {
    private Toolbar toolbar = null;
    private DrawerLayout drawerLayout = null;
    private ActionBarDrawerToggle drawerToggle = null;;
    private ListView listView = null;
    private CharSequence drawerTitle = null;
    private CharSequence title = null;
    private NavigationDrawerConfiguration navigationConfig = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.navigationConfig = this.getNavigationDrawerConfiguration();
        this.setContentView(this.navigationConfig.getMainLayout());

        this.toolbar = (Toolbar) this.findViewById(R.id.app_bar);
        this.drawerLayout = (DrawerLayout) this.findViewById(navigationConfig.getDrawerLayoutId());
        this.listView = (ListView) this.findViewById(navigationConfig.getLeftDrawerId());
        this.listView.setAdapter(navigationConfig.getBaseAdapter());
        this.listView.setOnItemClickListener(new DrawerItemClickListener());
        this.drawerTitle = this.getTitle();
        this.title = this.getTitle();

        this.setSupportActionBar(this.toolbar);
        this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        this.getSupportActionBar().setHomeButtonEnabled(true);

        this.drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, this.toolbar, this.navigationConfig.getDrawerOpenDesc(), this.navigationConfig.getDrawerCloseDesc()) {
            @Override
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(title);
                invalidateOptionsMenu();
            }
            @Override
            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(drawerTitle);
                invalidateOptionsMenu();
            }
        };

        this.drawerLayout.setDrawerListener(this.drawerToggle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        this.drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        this.drawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (this.navigationConfig.getActionMenuItemsToHideWhenDrawerOpen() != null) {
            boolean drawerOpen = this.drawerLayout.isDrawerOpen(listView);

            for (int item : this.navigationConfig.getActionMenuItemsToHideWhenDrawerOpen()) {
                menu.findItem(item).setVisible(!drawerOpen);
            }
        }

        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (this.drawerToggle.onOptionsItemSelected(item)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            if (this.drawerLayout.isDrawerOpen(this.listView)) {
                this.drawerLayout.closeDrawer(this.listView);
            } else {
                this.drawerLayout.openDrawer(this.listView);
            }

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    protected NavigationDrawerConfiguration getNavigationDrawerConfiguration() {

        NavigationDrawerItem[] menu = new NavigationDrawerItem[] {
//              NavigationMenuSection.create(100, "Demos"),
                NavigationMenuItem.create(100, "Item 1", "ic_measure", false, this),
                NavigationMenuItem.create(200, "Item 2", "ic_list", false, this),
//              NavigationMenuSection.create(200, "General"),
                NavigationMenuItem.create(300, "Item 3", "ic_diagrams", false, this),
                NavigationMenuItem.create(400, "Item 4", "ic_calculator", false, this),
                NavigationMenuItem.create(500, "Item 5", "ic_scanner", false, this),
                NavigationMenuItem.create(600, "Item 6", "ic_profile", false, this),
                NavigationMenuItem.create(700, "Item 7", "ic_follower", false, this),
                NavigationMenuItem.create(800, "Item 8", "ic_settings", false, this)
        };

        NavigationDrawerConfiguration navigationDrawerConfiguration = new NavigationDrawerConfiguration();
        navigationDrawerConfiguration.setMainLayout(R.layout.activity_menu_slide);
        navigationDrawerConfiguration.setDrawerLayoutId(R.id.menu_slide_layout);
        navigationDrawerConfiguration.setLeftDrawerId(R.id.menu_slide_list);
        navigationDrawerConfiguration.setNavigationItems(menu);
        navigationDrawerConfiguration.setDrawerOpenDesc(R.string.open_navigation_drawer);
        navigationDrawerConfiguration.setDrawerCloseDesc(R.string.close_navigation_drawer);
        navigationDrawerConfiguration.setBaseAdapter(new NavigationDrawerAdapter(this, R.layout.activity_menu_slide_item_row, menu));

        return navigationDrawerConfiguration;
    }

    public void selectItem(int position) {
        NavigationDrawerItem selectedItem = this.navigationConfig.getNavigationItems()[position];

        this.onNavigationItemSelected(selectedItem.getId());
        this.listView.setItemChecked(position, true);

        if (selectedItem.updateActionBarTitle()) {
            this.setTitle(selectedItem.getLabel());
        }

        if (this.drawerLayout.isDrawerOpen(this.listView)) {
            this.drawerLayout.closeDrawer(this.listView);
        }
    }

    protected void onNavigationItemSelected(int id) {
        Intent intent = null;

        switch ((int) id) {
        case 100:
            intent = new Intent(this, MeasureDataActivity.class);
            break;
        case 200:
            intent = new Intent(this, MeasureDataListActivity.class);
            break;
        case 300:
            intent = new Intent(this, DiagramsActivity.class);
            break;
        case 400:
            intent = new Intent(this, CalcActivity.class);
            break;
        case 500:
            intent = new Intent(this, RecipeActivity.class);
            break;
        case 600:
            intent = new Intent(this, ProfileActivity.class);
            break;
        case 700:
            intent = new Intent(this, FollowerActivity.class);
            break;
        case 800:
            intent = new Intent(this, SettingsActivity.class);
            break;
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        this.startActivity(intent);
    }

    protected int getDrawerIcon() {
        return R.drawable.ic_navigation_drawer;
    }

    protected DrawerLayout getDrawerLayout() {
        return this.drawerLayout;
    }

    protected ActionBarDrawerToggle getDrawerToggle() {
        return this.drawerToggle;
    }

    @Override
    public void setTitle(CharSequence title) {
        this.title = title;
        this.getSupportActionBar().setTitle(title);
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        /**
         * 
         */
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }
}
Mulgard
  • 9,877
  • 34
  • 129
  • 232

1 Answers1

1

I think in the onCreate after the super() call you can do this:

mDrawerToggle.setDrawerIndicatorEnabled(false);

If this alone doesn't work (or just doesn't work) try this:

getActionBar().setDisplayHomeAsUpEnabled(true);

Follow this link: Missing Up navigation icon after switching from ICS ActionBar to Lollipop Toolbar

Original poster found this link

Community
  • 1
  • 1
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76
  • no this does not work. if i disable the DrawerIndicator i cant press anyhting in the toolbar. if i add DisplayHomeAsUpEnabled(true) there is also nothing to press in the toolbar. Its the same using the combination of both. – Mulgard Dec 13 '14 at 14:45
  • @Mulgard I just created my own project and if I do `mDrawerToggle.setDrawerIndicatorEnabled(false);` it displays a back button. But you mentioned that you have a toolbar, do you have some sample code? – Kevin van Mierlo Dec 13 '14 at 15:01
  • Yes i added the sample code for the NavigationDrawer. I used Toolbar because all ActionBar stuff is deprecated since 5.0 – Mulgard Dec 13 '14 at 15:24
  • @Mulgard What ActionBar stuff is deprecated? Because the Toolbar is just used as an ActionBar if you want more control over its appearance. So if you don't need to have more control over that, I'd suggest you switch back to ActionBar – Kevin van Mierlo Dec 13 '14 at 15:30
  • I hade problems with tabs in 5.0 similar to this post: http://stackoverflow.com/questions/24473213/action-bar-navigation-modes-are-deprecated-in-android-l. And yes, my designer wants me to have more control over appearance ^^ – Mulgard Dec 13 '14 at 15:33
  • i found something: http://stackoverflow.com/questions/26549008/missing-up-navigation-icon-after-switching-from-ics-actionbar-to-lollipop-toolba – Mulgard Dec 13 '14 at 15:40