在OnCreate中以编程方式生成MapFragment.我通过GetMapAsync在OnResume中获取GoogleMap,并在OnMapReady中绑定侦听器.
他们工作正常,但只在一开始.一旦设备旋转(纵向 – >横向或反之亦然),相机移动不会再触发听众.
然而,地图工作 – 我(用户)仍然可以移动相机.我(应用程序)不能再用它了.
这是裸码,只有地图创建和处理.其他(实际绘图)被删除:
public class MapActivity : Activity,IOnMapReadyCallback,GoogleMap.IOnCameraIDleListener,GoogleMap.IOnCameraMoveStartedListener{ private GoogleMap _map; private MapFragment _mapFragment; private voID InitializeMap() { _mapFragment = MapFragment.NewInstance(); var tx = FragmentManager.BeginTransaction(); tx.Add(Resource.ID.map_placeholder,_mapFragment); tx.Commit(); } private voID SetMapListeners() { Log.DeBUG("MyApp/ Map","SetMapListeners"); _map.SetonCameraIDleListener(this); _map.SetonCameraMoveStartedListener(this); } /* Activity */ protected overrIDe voID OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Log.DeBUG("MyApp / Map","OnCreate"); SetContentVIEw(Resource.Layout.Map); InitializeMap(); } protected overrIDe voID OnStart() { base.OnStart(); Log.DeBUG("MyApp / Map","OnStart"); } protected overrIDe voID OnResume() { base.OnResume(); if (_map == null) _mapFragment.GetMapAsync(this); Log.DeBUG("MyApp / Map","OnResume"); } protected overrIDe voID OnPause() { base.OnPause(); Log.DeBUG("MyApp / Map","OnPause"); } protected overrIDe voID OnStop() { base.OnStop(); Log.DeBUG("MyApp / Map","OnStop"); } protected overrIDe voID OnDestroy() { base.OnStop(); Log.DeBUG("MyApp/ Map","OnDestroy"); } /* IOnMapReadyCallback */ public voID OnMapReady(GoogleMap GoogleMap) { Log.DeBUG("MyApp / Map","Map is ready!"); _map = GoogleMap; SetMapListeners(); } /* IOnCameraIDleListener */ public voID OnCameraIDle() { Log.DeBUG("MyApp / Map","Camera is IDle."); // Drawing routine is called here } /* IOnCameraMoveStartedListener */ public voID OnCameraMoveStarted(int reason) { Log.DeBUG("MyApp / Map","Camera move started."); }}
正如您在以下日志摘录中可以看到的,侦听器在开始时工作,但是一旦设备旋转(至少)一次,它们就会消失.
我也尝试在生命周期中调用SetMapListeners一次,第一次调用OnMapReady,但是没有改变任何东西.
04-03 20:29:06.486 D/MyApp / Map( 7446): OnCreate04-03 20:29:06.688 I/Google Maps AndroID API( 7446): Google Play services clIEnt version: 1008400004-03 20:29:06.695 I/Google Maps AndroID API( 7446): Google Play services package version: 1029843804-03 20:29:07.394 D/MyApp / Map( 7446): OnStart04-03 20:29:07.399 D/MyApp / Map( 7446): OnResume04-03 20:29:07.432 D/MyApp / Map( 7446): Map is ready!04-03 20:29:07.438 D/MyApp / Map( 7446): SetMapListeners04-03 20:29:07.568 D/MyApp / Map( 7446): Camera is IDle.04-03 20:29:09.231 D/MyApp / Map( 7446): Camera move started.04-03 20:29:09.590 D/MyApp / Map( 7446): Camera is IDle.04-03 20:29:12.350 D/MyApp / Map( 7446): Camera move started.04-03 20:29:12.751 D/MyApp / Map( 7446): Camera is IDle.## Listeners are responding,Now rotating the device.04-03 20:29:15.503 D/MyApp / Map( 7446): OnPause04-03 20:29:15.508 D/MyApp / Map( 7446): OnStop04-03 20:29:15.572 D/MyApp / Map( 7446): OnDestroy04-03 20:29:15.595 I/Google Maps AndroID API( 7446): Google Play services package version: 1029843804-03 20:29:15.596 D/MyApp / Map( 7446): OnCreate04-03 20:29:15.628 I/Google Maps AndroID API( 7446): Google Play services package version: 1029843804-03 20:29:15.655 D/MyApp / Map( 7446): OnStart04-03 20:29:15.655 D/MyApp / Map( 7446): OnResume04-03 20:29:15.690 D/MyApp / Map( 7446): Map is ready!04-03 20:29:15.691 D/MyApp / Map( 7446): SetMapListeners## Map is rotated,camera position was preserved. ## Now moving the camera,but no Listeners are responding.04-03 20:29:24.436 D/MyApp / Map( 7446): OnPause04-03 20:29:31.288 D/MyApp / Map( 7446): OnStop04-03 20:29:31.359 D/MyApp / Map( 7446): OnDestroy
对我来说有趣的是,当我切换回上一个活动并再次打开地图时,它会开始新鲜,再次工作.但是,正如您在日志中看到的那样,在旋转设备的同时,活动也被破坏并新创建.据我所知,片段不是,所以也许这是暗示.我不知道.
我也尝试删除OnDestroy中的监听器(通过设置null),但也没有改变任何东西.
你有什么想法我可能做错了吗?
解决方法 尝试检查savedInstanceState以查看您的Activity是否是第一次构建,或者是否只是返回它.您似乎在每次呼叫时都是重叠的片段. 总结以上是内存溢出为你收集整理的c# – 旋转设备后如何保存/重新绑定MapFragment上的事件侦听器(纵向/横向)?全部内容,希望文章能够帮你解决c# – 旋转设备后如何保存/重新绑定MapFragment上的事件侦听器(纵向/横向)?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)