setContentView() slow with map fragment
Asked Answered
R

2

13

I am working on an app that shows a google map (api v2) as a fragment. When the app loads it shows a blank white screen for a couple of seconds before showing the map. I have used log statements see where the delays is but I don't know why it's so slow.

Here is my onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate Start -------------------------------");
    super.onCreate(savedInstanceState);
    Log.i(TAG, "onCreate 1 -------------------------------");
    setContentView(R.layout.activity_main);
    Log.i(TAG, "onCreate 2 -------------------------------");
    do_async_setup();
    Log.i(TAG, "onCreate 3 -------------------------------");
    prefs = getSharedPreferences(PREFS_NAME, 0);
    prefs_editor = prefs.edit();
    Log.i(TAG, "onCreate Finish -------------------------------"); 

And here is the output.

01-20 10:05:28.802: I/HeadsUp(19544): onCreate Start -------------------------------
01-20 10:05:28.802: I/HeadsUp(19544): onCreate 1 -------------------------------
01-20 10:05:30.396: I/HeadsUp(19544): onCreate 2 -------------------------------
01-20 10:05:30.396: I/HeadsUp(19544): onCreate 3 -------------------------------
01-20 10:05:30.403: I/HeadsUp(19544): onCreate Finish -------------------------------

You can see a 1.5 sec delay for setContentView. Here is my layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>

It's simply looks bad, like the app has frozen when loading. How can I either speed this up or hide the delay?

thanks,

Rebbecarebbecca answered 19/1, 2013 at 21:13 Comment(9)
Use Traceview to figure out specifically where your problem is.Rodl
@Rodl , if I am reading it corectly it tells me what the logs told me. setContentView is using approx 44% of the 'time' for the onCreate method. Incl Real Time says 3211.060 which I assume are milliseconds? Also, it's ActionBarSherlockNative.setContentView() could that be why it's so slow loading the xml etc? Stepping down through the longest running child methods I get to LayoutInflater.createViewFromTag and and a few more steps SupportMapFragment.onCreateView() still at 1998ms and then into some more google gms code. So it looks like google map fragment. does that seem right?Rebbecarebbecca
Well, the most likely culprit would be the map, so I suppose it is not surprising. That being said, it also suggests that you are possibly just stuck. What sort of hardware are you running this on?Rodl
@Rodl It's running on a galaxy nexus phone. I think you are right. There is a similar delay with goggles map app on the same device. I had looked at covering it with a splash dialog, but it does not get shown until after the delay so thats pointless, and I dont want a splash screen any way. Also its faster when not debugging.Rebbecarebbecca
@CommonsWare: i am not sure. But is it possible because of the internet connection speed ?Sapindaceous
It is likely the map content has been previously cached. If it's a fresh run with no cache and no network connection, the map will remain blank until connected (when it will be able to d/l the map contents). I think my answer is more likely the explanation to the question.Hickox
I noticed in the final (non-debug) build, the delay is far smaller: maybe ~200ms vs. 2000ms+ in debug. This is acceptable (just). But it would be nice to eliminate it, is there a way to pre-instantiate the activity in the background perhaps, or at least get the MapFragment / GLSurfaceView to initialise fully beforehand?Rowden
@JarrodSmith Did you ever find a way? My delay is almost 2000ms even in non-debug.Sourdough
@mattblang No I didn't explore the pre-instantiation idea very far, but to me it looked like it would only help if you went through a complete fragment transaction / setContentView() cycle.Rowden
H
13

This is a drawback of using a GLSurfaceView such as the new maps v2 in Android. The delay is caused by the initialisation of the GLSurfaceView. See here http://code.google.com/p/gmaps-api-issues/issues/detail?id=4639 for a brief explanation near post #5 from a Googler and a snippet to help this problem (a little). Please star the issue to increase its priority.

Hickox answered 20/1, 2013 at 22:26 Comment(1)
Thanks, that confirms it for me.Rebbecarebbecca
O
2

I've found that this can be pretty slow if you have a debugging session connected, but is usually a more acceptable speed when you don't. If it runs faster with the USB cable disconnected, that's probably why.

Olive answered 22/12, 2014 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.