How to improve fragment loading speed?

后端 未结 5 753
清歌不尽
清歌不尽 2020-12-04 14:38

Performance Enhancement:

Previously I saved ALL images in drawable folder, this might be the reason why the map first l

相关标签:
5条回答
  • 2020-12-04 15:07

    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();
    
    0 讨论(0)
  • 2020-12-04 15:18

    I had been running into this problem a lot too... Turns out the biggest culprit was having a debugging session attached to my app's process. The maps stuff in my app ran much faster and more smoothly when I disconnected the debugger, or just unplugged the USB cable.

    Check out my related post here: First launch of Activity with Google Maps is very slow

    0 讨论(0)
  • 2020-12-04 15:20

    Just to add to @Xyaren's answer, I needed it to work for SupportFragment which seemed to require some extra steps. Have your activity implement OnMapReadyCallback.

    In your onCreate:

    new Thread(() -> {
        try {
            SupportMapFragment mf = SupportMapFragment.newInstance();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.dummy_map_view, mf)
                    .commit();
            runOnUiThread(() -> mf.getMapAsync(SplashActivity.this));
        }catch (Exception ignored){
            Timber.w("Dummy map load exception: %s", ignored);
        }
    }).start();
    

    You'll have to implement this:

    @Override
    public void onMapReady(GoogleMap googleMap) {
        // Do nothing because we only want to pre-load map.
        Timber.d("Map loaded: %s", googleMap);
    }
    

    and in your activity layout:

    <FrameLayout
        android:id="@+id/dummy_map_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:visibility="gone"/>
    

    It needs to go through all the steps including the transaction for Google Play Services to download the map data.

    0 讨论(0)
  • 2020-12-04 15:20

    [EDITED]

    The getMap() and setUpMap() methods are probably very slow. Their processing should be done in an AsyncTask so that onCreateView() can return quickly every time.

    More info: onCreateView() is called on the UI thread, so any slow processing that it does will freeze the UI. Your AsyncTask should do all the slow processing in its doInBackground() method, and then update the UI in its onPostExecute() method. See the AsyncTask JavaDoc for more details.

    0 讨论(0)
  • 2020-12-04 15:30

    You could try this

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapActivity);
        getSreenDimanstions();
        fragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
        
        map = fragment.getMap();    
    }
    

    This class is extended from Activity

    0 讨论(0)
提交回复
热议问题