Google Maps v2 Android API blocks UI thread

Inside the FragmentActivity I replace the fragment nested in the TabPageIndicator (2 tabs inside).

The first tab is a fragment with a description.
The second tab - Fragment with a map inside + markers!

When I start FragmentActivity, the pause is about 3-8 seconds (depending on the performance of the phone).

The reason is the map display.

How to speed up the launch?


FragmentStatePagerAdapter

@Override public Fragment getItem(int position) { switch (position) { case 0: return new Fragment1(); case 1: return new Fragment2(); } return null; } 

FRAGMENT2

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i(Util.TAG, getClass().getName() + " onCreateView"); if (view != null) { ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) parent.removeView(view); } try { view = (ViewGroup) inflater.inflate(R.layout.map_view, container, false); } catch (InflateException e) { } setUpMapIfNeeded(); return view; } public void setUpMapIfNeeded() { if (map == null) { SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map); map = mapFragment.getMap(); if (map != null) { setUpMap(); } else { Toast.makeText(getActivity(), "Unable to create map", Toast.LENGTH_SHORT).show(); } } } private void setUpMap() { map.setMyLocationEnabled(true); map.addMarker(new MarkerOptions().position(KIEV).title("Kiev").snippet("Ukraine"));// .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)) map.moveCamera(CameraUpdateFactory.newLatLngZoom(KIEV, 15)); map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); map.setOnMapLoadedCallback(this); } 

I tried to comment on everything except this:

 view = (ViewGroup) inflater.inflate(R.layout.map_view, container, false); 

but the result is the same. Map initialization is too long.

Should I use a separate stream to load the map view? (If so, how can I get the layout and container in the background thread)

I also decided to download the map if the fragment is visible, but it was not a good idea.

+7
performance android api android-fragments google-maps
source share

No one has answered this question yet.

See related questions:

3606
Close / hide Android soft keyboard
3295
Why is the Android emulator so slow? How can we speed up Android emulator development?
3288
Correct use cases for Android UserManager.isUserAGoat ()?
2609
Is there a unique identifier for an Android device?
2510
How to keep Android activity state by saving instance state?
2097
Is there a way to run Python on Android?
1844
What is "Context" on Android?
286
Update ViewPager dynamically?
3
Failed to load map. Error communicating with Google servers on fragment
one
null pointer exception with listview.setadapter () in fragment in oncreateview method

All Articles