首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SupportMapFragment正在泄漏内存

SupportMapFragment正在泄漏内存
EN

Stack Overflow用户
提问于 2020-07-22 11:53:29
回答 1查看 818关注 0票数 2

我正在尝试新版本的GoogleMap

com.google.android.libraries.maps:maps:3.1.0-beta

但是当我回到以前的活动时,它似乎会漏出记忆。

我在活动中使用片段

以下是代码:

代码语言:javascript
复制
XML

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/map"
        android:name="com.google.android.libraries.maps.SupportMapFragment"
        android:layout_width="0dp"
        android:layout_height="220dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:useViewLifecycle="true"
        tools:context=".OutputScreen"/>

Java代码

代码语言:javascript
复制
    public class OutputScreen extends FragmentActivity implements OnMapReadyCallback{
    
    
    private GoogleMap map;
    private SupportMapFragment mapFragment;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
            //setting layout
    
            mapFragment = (SupportMapFragment) 
            getSupportFragmentManager().findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        }

        @Override
        public void onMapReady(GoogleMap googleMap) {
          map = googleMap;
        }

        @Override
        protected void onDestroy() {
          if (map != null){
              map.clear();
              map.setMyLocationEnabled(false)
              map = null;
          }
        }
    }

LeakCanary报告

代码语言:javascript
复制
   ┬───
│ GC Root: System class
│
├─ android.provider.FontsContract class
│    Leaking: NO (Application↓ is not leaking and a class is never leaking)
│    ↓ static FontsContract.sContext
├─ android.app.Application instance
│    Leaking: NO (Application is a singleton)
│    Application does not wrap an activity context
│    ↓ Application.mActivityLifecycleCallbacks
│                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
├─ java.util.ArrayList instance
│    Leaking: UNKNOWN
│    ↓ ArrayList.elementData
│                ~~~~~~~~~~~
├─ java.lang.Object[] array
│    Leaking: UNKNOWN
│    ↓ Object[].[5]
│               ~~~
├─ com.google.android.libraries.maps.dj.zzf instance
│    Leaking: UNKNOWN
│    ↓ zzf.zza
│          ~~~
├─ com.google.android.libraries.maps.dj.zzd instance
│    Leaking: UNKNOWN
│    ↓ zzd.zzd
│          ~~~
├─ com.google.android.libraries.maps.bm.zzu instance
│    Leaking: UNKNOWN
│    ↓ zzu.zzd
│          ~~~
├─ com.google.android.libraries.maps.kf.zzab instance
│    Leaking: UNKNOWN
│    ↓ zzab.zzh
│           ~~~
├─ com.google.android.libraries.maps.kf.zzd instance
│    Leaking: UNKNOWN
│    ↓ zzd.zzi
│          ~~~
├─ com.google.android.libraries.maps.ed.zzau instance
│    Leaking: YES (View detached and has parent)
│    mContext instance of android.app.Application, not wrapping activity
│    View#mParent is set
│    View#mAttachInfo is null (view detached)
│    View.mWindowAttachCount = 1
│    ↓ zzau.mParent
├─ android.widget.FrameLayout instance
│    Leaking: YES (zzau↑ is leaking and View detached and has parent)
│    mContext instance of android.app.Application, not wrapping activity
│    View#mParent is set
│    View#mAttachInfo is null (view detached)
│    View.mWindowAttachCount = 1
│    ↓ FrameLayout.mParent
╰→ android.widget.FrameLayout instance
​     Leaking: YES (ObjectWatcher was watching this because com.google.android.libraries.maps.SupportMapFragment received Fragment#onDestroyView() callback (references to its views should be cleared to prevent leaks) and View.mContext references a destroyed activity)
​     

我寻找了其他堆叠溢出的答案,但没有找到任何适当的解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-09 02:31:56

问题是,SupportMapFragment被合并到您的片段容器视图中,但是没有调用called。您需要手动调用它。

在OutputScreen类中,在onDestroy()方法下,需要调用以下命令:

代码语言:javascript
复制
mapFragment.onDestroy()  // call this in your activity's onDestroy
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63033583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档