27

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?

Community
  • 1
  • 1
Krupal Shah
  • 8,949
  • 11
  • 57
  • 93
  • It will synchronized the icon from the drawer and the drawer itself where when you move the drawer the icon rotate, try to remove the `syncState` and those animation wont work. I mean buggy – Rod_Algonquin May 02 '15 at 06:43
  • @Rod_Algonquin ya...I just noticed it...the animation isn't working when I comment this line...but hey, can you explain why it should be called within onPostCreate()? – Krupal Shah May 02 '15 at 06:45
  • 3
    As I said It needs to synchronized to enable the animation to work even if the activity has been restored and where to put it? the answer is the `onPostCreate` where it is called after the `onRestoreInstanceState` – Rod_Algonquin May 02 '15 at 06:59
  • @Rod_Algonquin i think you should post that as answer sir, its Bounty -(lifts eyebrows) :) – Elltz May 09 '15 at 20:43

4 Answers4

13

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

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • but what if you call if from `onResume()` ? that gets called before `onRestoreInstanceState` or it requires inputs from `onRestoreInstanceState` ?? – Elltz May 09 '15 at 22:20
  • @Elltz The `onResume` is called right after `onRestoreInstanceState` http://developer.android.com/reference/android/app/Activity.html#onResume() – Rod_Algonquin May 09 '15 at 22:38
  • 1
    @Elltz In addition to this, read the method comment here: [syncState()](http://androidxref.com/5.1.0_r1/xref/frameworks/support/v4/java/android/support/v4/app/ActionBarDrawerToggle.java#291) – Vikram May 10 '15 at 01:01
  • that actually sums all doubts, pretty clear answer thanks@Vikram – Elltz May 10 '15 at 11:09
13

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?

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
1

You need to call syncState() from your activity's onPostCreate to set the state of the indicator (icon + drawer itself) based on whether the drawer layout is in open or closed state once the activity has been restored with onRestoreInstanceState.

Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
0

Simply say: SyncState() is to sync the toggle state after onRestoreInstanceState has occurred. And call it in onPostCreate(...) is because of that onPostCreate(...) is called just after onRestoreInstanceState(...) is called.

Xcihnegn
  • 11,579
  • 10
  • 33
  • 33