Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Drawer - what does syncState() do and why it should be called inside onPostCreate()?

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?

like image 780
Krupal Shah Avatar asked May 02 '15 06:05

Krupal Shah


People also ask

What is syncState in android studio?

A table provided for sync adapters to use for storing private sync state data for contacts. See also: SyncStateContract.

What is onPostCreate?

using onPostCreate as a callback method from subclass' onCreate will notify that all creation is finished. Example: If you have activities sharing the same layout, then you can use the onPostCreate to add onClickListeners etc. If you are overriding onPostCreate, it is best to make the call to super.

What is a DrawerLayout?

DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.

What is ActionBarDrawerToggle?

androidx.appcompat.app.ActionBarDrawerToggle. This class provides a handy way to tie together the functionality of DrawerLayout and the framework ActionBar to implement the recommended design for navigation drawers.


1 Answers

Well, I think of this question as a good one. And I will collect this question and its answers. So, let's do some summaries here:

First, as to ActionBarDrawerToggle.syncState(), just as the document 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.)

Second, as to Activity.onPostCreate(Bundle), it is called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called). Applications will generally not implement this method; it is intended for system classes to do final initialization after application code has run.

But, it is derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

So, What does syncState() exactly do?

Well, ActionBarDrawerToggle.syncState() will synchronize the changed icon's state, which deponds on actions of DrawerLayout. If you ever tried to remove the syncState(), you will realize that the icon of arrow won't rotate any more.

And why syncState() should be called inside onPostcreate()?

Well, onPostcreate() is called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called), while at the moment, Activity needs animations to be ready to work. So, if it isn't the best moment for animations, when is it?

like image 69
SilentKnight Avatar answered Sep 18 '22 08:09

SilentKnight