关于java:SupportMapFragment在主线程上加载并冻结接口(忽略getMapAsync)

SupportMapFragment loading on the main thread and freezing the interface (getMapAsync ignored)

在默认的SupportMapFragment中,在实例化Fragment时将加载并创建MapView,从而在主线程上进行了大量工作。

下面的日志发生在调用getMapAsync(this);之前,这是主线程冻结大约2秒钟的时刻。日志以某种方式证明了getMapAsync()

之前的所有内容都已创建。

不应该仅在调用getMapAsync()时加载地图吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Enabling debug mode 0
D/OpenGLRenderer: endAllActiveAnimators on 0xb7966f90 (RippleDrawable) with handle 0xb7988688
I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
I/zzy: Making Creator dynamically
W/ResourcesManager: Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
W/ResourcesManager: Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
W/art: Suspending all threads took: 5.704ms
D/ChimeraCfgMgr: Loading module com.google.android.gms.maps from APK /data/data/com.google.android.gms/app_chimera/chimera-module-root/module-fcfb08c37b9ca44c48d9936b0e1895ef8b9cffe0/MapsModule.apk
D/ChimeraModuleLdr: Loading module APK /data/data/com.google.android.gms/app_chimera/chimera-module-root/module-fcfb08c37b9ca44c48d9936b0e1895ef8b9cffe0/MapsModule.apk
D/ChimeraFileApk: Primary ABI of requesting process is armeabi-v7a
D/ChimeraFileApk: Classloading successful. Optimized code found.
I/GoogleMapsAndroidAPI: Google Play services client version: 7895000
I/GoogleMapsAndroidAPI: Google Play services package version: 8115236
I/e: Token loaded from file. Expires in: 393978062 ms.
I/e: Scheduling next attempt in 393678 seconds.
I/Choreographer: Skipped 44 frames!  The application may be doing too much work on its main thread.

这是片段:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class StoreMapFragment extends SupportMapFragment
        implements OnMapReadyCallback {

    private GoogleMap mMap;

    public static StoreMapFragment newInstance() {
        StoreMapFragment fragment = new StoreMapFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getMapAsync(this);
    }

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }

    public void loadStores() {
        if(mMap == null)
            return;

        // Add markers
    }
}

相关:

  • 首次在Google Maps中启动Activity的速度很慢
  • 是什么使我的地图片段加载缓慢?

通过延迟片段实例化解决。

首先,我在另一个Fragment中添加了SupportMapFragment,然后按照此处的建议延迟了SupportMapFragment实例化。

StoreMapFragment.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class StoreMapFragment extends Fragment
        implements OnMapReadyCallback {

    SupportMapFragment mMapFragment;
    private GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_map, container, false);

        if (mMap == null) {
            final OnMapReadyCallback listener = this;
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    if (isAdded()) {
                        mMapFragment = new SupportMapFragment();
                        FragmentManager fm = getChildFragmentManager();
                        fm.beginTransaction()
                                .replace(R.id.map, mMapFragment).commit();
                        mMapFragment.getMapAsync(listener);
                    }
                }
            }, 1000);
        }

        return view;
    }

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

fragment_map.xml

1
2
3
4
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


您应该使用onViewCreated()方法获取地图:

1
2
3
4
5
6
7
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mMap = (SupportMapFragment) getChildFragmentManager().
            findFragmentById(R.id.map);
    mMap.getMapAsync(this);
}

这不会导致加载地图的任何延迟。