55274

Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in

I have refined the Navigation Drawer Activity project template of Android Studio, which uses <strong>Toolbar</strong>, <strong>v7.app.ActionBarDrawerToggle</strong> and <strong>NavigationView</strong> instead of the NavigationDrawerFragment (and layout/fragment_navigation_drawer.xml).

According to Google's guidance and reference, I set up ActionBarDrawerToggle. I made it 1) instantiate in onCreate, 2) call syncState in onPostCreate and 3) call through to onConfigurationChanged and onOptionsItemSelected.

It is almost perfectly working except for one thing: <strong>the hamburger icon is showed as an arrow</strong>.

A similar issue can be found on the StackOverFlow, especially for this question. But the question is about the way using the old R.drawable.ic_drawer as hamburger which is not the material design (before 5.0 Lollipop) version. Moreover the question has no answer and the questioner commented he had solved without stating any solution.

After a while, I have found a solution accidentally. It is somewhat dirty. It is to call syncState in onCreate. Because it seems that, for some reason, onPostCreate is not called in my app. Actually, this dirty solution is used in an answer to the other problem.

But the official reference says to call syncState in onPostCreate. Why doesn't it work? Why doesn't my app call onPostCreate? This should be main cause of not being showed hamburger icon (instead being showed arrow).

Below is my code:

@Override protected void onCreate(Bundle savedInstanceState) { ... toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); drawerToggle = new ActionBarDrawerToggle( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); invalidateOptionsMenu(); // calls onPrepareOptionsMenu() } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); invalidateOptionsMenu(); // calls onPrepareOptionsMenu() } }; drawerLayout.setDrawerListener(drawerToggle); navigationView = (NavigationView) findViewById(R.id.navigation_view); navigationView.setNavigationItemSelectedListener(this); drawerToggle.syncState(); // calling this here is somewhat a dirty solution } @Override public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) { super.onPostCreate(savedInstanceState, persistentState); drawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); drawerToggle.onConfigurationChanged(newConfig); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (drawerToggle.onOptionsItemSelected(item)) { return true; } ... }

Answer1:

Here onPostCreate:

@Override public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) { super.onPostCreate(savedInstanceState, persistentState); drawerToggle.syncState(); }

It should be this:

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

There are two types of onPostCreate:

<ol> <li>Activity's onPostCreate with two arguments.</li> <li>AppCompatActivity's onPostCreate with a single argument.</li> </ol>

You should have made a mistake to choose the former one when you override a method on Android Studio.

Recommend

  • Key board appears every time when it open the home activity of my app,UNABLE to HIDE KEYBOARD
  • Cannot connect to beagle bone black
  • How to close bootstrap 3 dropdown when we click outside on a tablet?
  • App crashed after adding navigation menu in Android Studio
  • Moving through toggle buttons using arrow keys
  • Troubles using jquery get to retrieve data from the server and place in a div
  • Add Active Nav Link Class In Pug
  • How to check the text of an NSTextField (Label)
  • Sequelize - Join with multiple column
  • Generating and downloading an excel file generates a ERR_INVALID_RESPONSE
  • Grow Animation for Floating Action Button
  • Dynamically creating HTML button control and nesting i tag within it
  • SceneKit: Is it possible to cast an shadow on an Transparent Object?
  • Android ripple for button not working
  • Android how to set a transparent image resource of a view? or Remove the image?
  • Change Design Support Navigation View Header Title Programmatically
  • cannot load gems in test environment
  • How to stop a goroutine that is listening for RethinkDB changefeeds?
  • Making Cross Site Asynchronous HTTP Post from GWT Client
  • Connect Node.js with Oracle on Windows platform
  • write text on image and show it to a imageview
  • Unable to resolve Static method?
  • C#: Import/Export Settings into/from a File
  • How to set elevation color?
  • Can I programmatically choose the Android layout folder?
  • Validate child input components on submit with Vee-Validate and vue js 2
  • Blackberry - Custom EditField Cursor
  • Ionic 2 storage is not cleaning up on uninstall - Only for signed APK
  • Content-Length header not returned from Pylons response
  • $wpdb not working in file of WordPress plugin
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Display issues when we change from one jquery mobile page to another in firefox
  • Different response to non-authenticated users and AJAX calls
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Controls, properties, events and timers running in design time
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Arrays break string types in Julia
  • Data Validation Drop Down Box Arrow Disappearing
  • reshape alternating columns in less time and using less memory