如何仅在初始加载时将 MapView 居中?
我正在使用 MapActivity 开发 Android 应用程序,但在以适当的方式管理 MapView 状态时遇到问题。
基本上,我有一个活动在 MapActivity 退出时启动它,此时我需要将地图居中。在查看 MapActivity 的所有其他时间,我需要它基本上保留其最后的状态,并且不更改其位置或缩放级别或任何内容。只要 MapActivity 没有完全销毁,导航到其他活动时它就可以正常工作,但是我需要地图在设备方向发生变化时保留其状态。大多数 Android 开发人员都知道,当发生这种情况时,Activity 会被销毁并重新创建,此时我的 MapView 会重新居中,但我不希望它这样做。
如何安排我的代码,以便当 MapActivity 从我的其他活动启动时可以执行我的居中例程,否则不执行我的居中例程?
I'm working on an Android application with a MapActivity and I'm having trouble managing the MapView state in an appropriate manner.
Basically I have an activity that starts the MapActivity when it exits and at this point I need to center the map. All other times that the MapActivity is viewed I need it to basically retain it's last state and not change its position or zoom level or anything. It works fine when navigating to other activities as long as the MapActivity is not destroyed completely however I need the map to retain it's state when the device orientation changes. As most Android developers know the Activity is destroyed and re-created when that happens, at which point my MapView re-centers and I don't want it to.
How can I arrange my code so that I can execute my centering routine when the MapActivity is started from my other activity, and not execute my centering routine otherwise?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从
onRetainNonConfigurationInstance()
返回非null
的内容。然后,无论您将地图置于中心位置,都可以调用getLastNonConfigurationInstance()
。如果为null
,则居中;如果它是非null
,则不居中。Return something non-
null
fromonRetainNonConfigurationInstance()
. Then, wherever you are centering the map, callgetLastNonConfigurationInstance()
. If it'snull
, center; if it is non-null
, don't center.