【问题标题】:Unable to destroy supportMapFragment on onDestroy无法在 onDestroy 上销毁 supportMapFragment
【发布时间】:2016-06-24 10:46:32
【问题描述】:

我正面临 OOM 问题,因为 supportMapFragment 没有被破坏。

使用的Xml:

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

代码 sn-p:

mMapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
GoogleMapOptions mapOptions = new GoogleMapOptions();
mapOptions.useViewLifecycleInFragment(true);
mMapFragment.newInstance(mapOptions);

//ondestroy调用

@Override
protected void onDestroy() {
    super.onDestroy();

    //clear all the data
    mMap.clear();
    mMapFragment.onDestroyView();

}

遵循与给定文档中提到的相同: https://developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment

【问题讨论】:

  • 问题是,onDestroy() 可能不会被调用 - 不能保证它会被调用。
  • 在销毁活动时调用检查部分..

标签: android google-maps-android-api-2 markerclusterer


【解决方案1】:

据我所知,您没有在此处为您的 mMapFragment 设置选项:

mMapFragment.newInstance(mapOptions);

您正在使用此选项再创建一个 SupportMapFragment,但它丢失了,因为您没有分配它。 Documentation says,即 GoogleMapsOptions

为 GoogleMap 定义配置 GoogleMapOptions。这些选项 可以在以编程方式将地图添加到应用程序时使用(如 反对通过 XML)。

如果您想调用 OnDestroy - 您应该以编程方式创建地图,无需 XML,例如:

GoogleMapOptions mapOptions = new GoogleMapOptions();
mapOptions.useViewLifecycleInFragment(true);
mMapFragment = SupportMapFragment.newInstance(mapOptions);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    相关资源
    最近更新 更多