Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

android - How to improve fragment loading speed?

Performance Enhancement:

Previously I saved ALL images in drawable folder, this might be the reason why the map first loads slow, when draw the markers on screen, the image may not fit the screen size. Now I saved images in drawable-mdpi, drawable-hdpi and so on, the app works smoother than before. Hope it helps

Original Question:

I created a map in a fragment, the source code can be found below.

The map fragment is sluggish when the first time it loads. If I go any other fragment and click the map fragment again, it loads fast and no slug anymore.

Can anyone tell me what is going on here? Thanks!

fragment_map.xml, id is map

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

MyMapFragment.java (contains onCreateView and setUpMapIfNeeded)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    try {
        rootView = inflater.inflate(R.layout.fragment_map, container, false);
    } catch (InflateException e) {
    /* map is already there, just return view as it is */
        Log.e(TAG, "inflateException");
    }

    setUpMapIfNeeded();
     
    return rootView;
}


public void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the fragment_map.
    if (myMap == null) {
        // Try to obtain the fragment_map from the SupportMapFragment.
        myMap = ((SupportMapFragment) MainActivity.fragmentManager.findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the fragment_map.
        if (myMap != null) {
            setUpMap();
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm using a very hackish but effective way in my application, but it works good. My mapFragment is not displayed right after the app launches! Otherwise this would not make sense.

Put this in your launcher activity's onCreate:

    // Fixing Later Map loading Delay
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                MapView mv = new MapView(getApplicationContext());
                mv.onCreate(null);
                mv.onPause();
                mv.onDestroy();
            }catch (Exception ignored){

            }
        }
    }).start();

This will create a mapview in an background thread (far away from the ui) and with it, initializes all the google play services and map data.

The loaded data is about 5MB extra.

If someone has some ideas for improvements feel free to comment please !


Java 8 Version:

// Fixing Later Map loading Delay
new Thread(() -> {
    try {
        MapView mv = new MapView(getApplicationContext());
        mv.onCreate(null);
        mv.onPause();
        mv.onDestroy();
    }catch (Exception ignored){}
}).start();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...