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"));
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.
performance android api android-fragments google-maps
Denis Zhuchinski
source share