Question

I have an activity which displays google map with some additional information. Also I have a service that downloads information from server and periodically informs activity that another portion of information is loaded using PendingIntent.
On Android 4.0.3 everything works fine. But on Android 2.3.4 map in activity blinks (goes grey and than reloads) every time service informs this activity.
Tried to track down if map is updated anywhere in onActivityResult, but it doesn't.
Any help appreciated. Here is some code:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == SERVICE_CACHED) {
        String fmId = data.getStringExtra("cachedId");
        if (dev.get(position).getId().equals(fmId)) {   //we still viewing the same device
            long[] dat = data.getLongArrayExtra("dates");
            ArrayList<Date> dates = new ArrayList<Date>();
            for (int i=0; i<dat.length; i++) {
                dates.add(new Date(dat[i]));
            }

            addDates(fmId, dates, false, true);

//this is executed only once, as it meant to be
            if ((dispDate.getText() == null) || (dispDate.getText().toString().isEmpty())) {
                State state;
                synchronized(DBHelper.class) {
                    state = DBHelper.getState(fmId, dates.get(0).getTime());
                }


                updDispState(state);
            }
        }
    }
}


public void addDates(String fmId, ArrayList<Date> dates, boolean clear, boolean addTimesTo0) {
    if (dev.get(position).getId().equals(fmId)) {   //maybe user already changed selected device
        if (clear) {
            this.dates = dates;
        } else {
            this.dates.addAll(0, dates);
        }
        updDateSpinners(clear, dates, addTimesTo0);
    }
}

Service informs activity using this:

private void cache(LinkedList<State> statsAll, LinkedList<Long> datesAll) {
        if ((statsAll == null) || (statsAll.isEmpty()) || (datesAll == null) || (datesAll.isEmpty())) {
            return;
        }

        synchronized(DBHelper.class) {
            DBHelper.addStates(statsAll, fmId);
        }
        //inform MapActivity that new data is available
        long[] l = new long[datesAll.size()];
        for (int i=0; i<l.length; i++) {
            l[i] = datesAll.get(i);
        }
        Intent intent = new Intent().putExtra("cachedId", fmId).putExtra("dates", l);
        try {
            pi.send(CacheService.this, MapActivity.SERVICE_CACHED, intent);
        } catch (PendingIntent.CanceledException e) {}          
    }

UPDATE
Doing some more testing I discovered that it's just map reloading itself on every onResume() call. How to stop it?
I put map in xml like this:

<fragment             
    android:id="@+id/mapF"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" /> 

And retrieve it in code later

GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapF)).getMap();    
Was it helpful?

Solution

You shouldn't use onActivityResult in this way from a service. Look into using a BroadcastReceiver instead. Your Activity class should register a broadcast listener, and your Service should send a broadcast Intent when new information is available.

OTHER TIPS

I have similar problem when I move the map (showing a menu) in android 4.0 it works fine but in 2.3 happens what you said. I couldn't do anything to fix it so my solution was use a relative layout and show the information in front of the map. I think that in "old versions" of android the map is not optimized for update in background and this is why he go black and update.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top