Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour of an activity on different android versions: map blinks

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();    
like image 949
Natalia Avatar asked Nov 13 '22 03:11

Natalia


1 Answers

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.

like image 171
twaddington Avatar answered Nov 15 '22 00:11

twaddington