I am learning to create navigation drawer in Android. While reading this, I can't understand following code:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
The documentation says:
Synchronize the state of the drawer indicator/affordance with the linked DrawerLayout.
This should be called from your Activity's onPostCreate method to synchronize after the DrawerLayout's instance state has been restored, and any other time when the state may have diverged in such a way that the ActionBarDrawerToggle was not notified. (For example, if you stop forwarding appropriate drawer events for a period of time.)
Further I read about onPostCreate()
from sstn's answer here: OnPostCreate in Fragment
onPostCreate() is mainly intented for framework use (although you can override it). The docs say that it is called after onStart() and onRestoreInstanceState().
This might lead to the assumption that it might be called before onResume() and thus probably before the message loop is dispatching events (including AsyncTask's onPostExecute() method), meaning your onPostExecute() will only fire after onPause().
As onPostCreate() is not properly documented and not really intended for application use - I might want to say it is not a good idea to rely on any observed behaviour.
From these two I couldn't understand anything. What does syncState()
exactly do and why it should be inside onPostcreate()
? Can anyone explain it better?
what does syncState()
It will synchronize the drawer icon that rotates when the drawer is swiped gestured left or right and if you try to removed the syncState()
the synchronization will fail thus resulting to buggy rotation or it wont even work.
why it should be called inside onPostCreate()?
It is called in the onPostCreate
to synchronize the animation all over again when the Activity
is restored. The good thing about onPostCreate
is that it is called right after onRestoreInstanceState
Edit:
As stated by @Vikram you can see the inline documentation of the method syncState