Navigation Drawer - syncState() 的作用是什么以及为什么要在 onPostCreate() 中调用它?
Navigation Drawer - what does syncState() do and why it should be called inside onPostCreate()?
我正在学习在 Android 中创建导航抽屉。在阅读 this 时,我无法理解以下代码:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
文档说:
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.)
此外,我从 sstn 的回答 here: OnPostCreate in Fragment
中了解到 onPostCreate()
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.
从这两个我什么都看不懂。 syncState()
到底是做什么用的,为什么它应该在 onPostcreate()
里面?谁能更好地解释一下?
您需要从 activity 的 onPostCreate 中调用 syncState()
来根据抽屉布局是处于打开状态还是关闭状态一次来设置指示器(图标 + 抽屉本身)的状态activity 已用 onRestoreInstanceState
恢复。
what does syncState()
当抽屉向左或向右滑动手势时,它将同步旋转的抽屉图标,如果您尝试删除 syncState()
,同步将失败,从而导致旋转错误或什至无法工作。
why it should be called inside onPostCreate()?
在onPostCreate
中调用,当Activity
恢复时,再次同步动画。 onPostCreate
的好处是它会在 onRestoreInstanceState
之后立即调用
编辑:
如@Vikram 所述,您可以查看方法的内联文档 syncState
简单的说:SyncState()就是在onRestoreInstanceState发生后同步toggle state。在 onPostCreate(...)
中调用它是因为 onPostCreate(...)
在 onRestoreInstanceState(...)
被调用之后被调用。
嗯,我认为这个问题很好。我会收集这个问题及其答案。那么,让我们在这里做一些总结:
首先,至于ActionBarDrawerToggle.syncState()
,正如文档所说,
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.)
第二个,至于Activity.onPostCreate(Bundle)
,在activity启动完成后调用(onStart()
和 onRestoreInstanceState(Bundle)
已被调用)。应用程序一般不会实现这个方法;它旨在供系统 classes 在应用程序代码具有 运行.
之后进行最终初始化
但是,它是派生的 classes 必须调用到 super class 这个方法的实现。如果他们不这样做,将抛出异常。
那么,syncState()
到底是做什么的?
嗯,ActionBarDrawerToggle.syncState()
会同步改变图标的状态,这取决于DrawerLayout
的动作。如果您曾经尝试删除 syncState()
,您会发现箭头图标不再旋转。
而为什么syncState()
里面要调用onPostcreate()
?
嗯,onPostcreate()
在 activity 启动完成时被调用(在 onStart()
和 onRestoreInstanceState(Bundle)
被调用之后),而此刻,Activity
需要动画才能工作。那么,如果现在不是动画的最佳时机,那是什么时候?
我正在学习在 Android 中创建导航抽屉。在阅读 this 时,我无法理解以下代码:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
文档说:
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.)
此外,我从 sstn 的回答 here: OnPostCreate in Fragment
中了解到onPostCreate()
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.
从这两个我什么都看不懂。 syncState()
到底是做什么用的,为什么它应该在 onPostcreate()
里面?谁能更好地解释一下?
您需要从 activity 的 onPostCreate 中调用 syncState()
来根据抽屉布局是处于打开状态还是关闭状态一次来设置指示器(图标 + 抽屉本身)的状态activity 已用 onRestoreInstanceState
恢复。
what does syncState()
当抽屉向左或向右滑动手势时,它将同步旋转的抽屉图标,如果您尝试删除 syncState()
,同步将失败,从而导致旋转错误或什至无法工作。
why it should be called inside onPostCreate()?
在onPostCreate
中调用,当Activity
恢复时,再次同步动画。 onPostCreate
的好处是它会在 onRestoreInstanceState
编辑:
如@Vikram 所述,您可以查看方法的内联文档 syncState
简单的说:SyncState()就是在onRestoreInstanceState发生后同步toggle state。在 onPostCreate(...)
中调用它是因为 onPostCreate(...)
在 onRestoreInstanceState(...)
被调用之后被调用。
嗯,我认为这个问题很好。我会收集这个问题及其答案。那么,让我们在这里做一些总结:
首先,至于ActionBarDrawerToggle.syncState()
,正如文档所说,
Synchronize the state of the drawer
indicator/affordance
with the linkedDrawerLayout
.This should be called from your
Activity
'sonPostCreate
method to synchronize after theDrawerLayout
's instance state has been restored, and any other time when the state may have diverged in such a way that theActionBarDrawerToggle
was not notified. (For example, if you stop forwarding appropriate drawer events for a period of time.)
第二个,至于Activity.onPostCreate(Bundle)
,在activity启动完成后调用(onStart()
和 onRestoreInstanceState(Bundle)
已被调用)。应用程序一般不会实现这个方法;它旨在供系统 classes 在应用程序代码具有 运行.
但是,它是派生的 classes 必须调用到 super class 这个方法的实现。如果他们不这样做,将抛出异常。
那么,syncState()
到底是做什么的?
嗯,ActionBarDrawerToggle.syncState()
会同步改变图标的状态,这取决于DrawerLayout
的动作。如果您曾经尝试删除 syncState()
,您会发现箭头图标不再旋转。
而为什么syncState()
里面要调用onPostcreate()
?
嗯,onPostcreate()
在 activity 启动完成时被调用(在 onStart()
和 onRestoreInstanceState(Bundle)
被调用之后),而此刻,Activity
需要动画才能工作。那么,如果现在不是动画的最佳时机,那是什么时候?