Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android freezes on ViewGroup.removeAllViews() with MapView

I've got a com.google.android.gms.maps.MapView that I create dynamically, using

mapView = new MapView(DataManager.getInstance().getContext());

This mapView gets attached to a parent through addView(mapView). Somewhere later in my program, I want to remove this mapView by calling parent.removeAllViews();, but there android just stalls. The app freezes completely, and I've got no idea why. What can I do to resolve this?

Edit:

I have done some more research using a CustomMapView:

import android.content.Context;
import android.view.View;

import com.google.android.gms.maps.MapView;

/**
 * @author Daniël van den Berg
 * @date 9/30/2015.
 */
public class CustomMapView extends MapView {
    public CustomMapView(Context context) {
        super(context);
    }

    @Override
    public void onViewRemoved(View child) {
        super.onViewRemoved(child);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }


    public void remove() {
        onPause();
        onDestroy();
    }
}

I call the remove method before trying to remove the view, and that's where it hangs. The breakpoint at onPause does get triggered, but the one at onDestroy doesn't. I call both onCreate() and onResume() when it gets created. Calling onDestroy() without calling onPause() does work, though then removeView(child) hangs again. So apparently onPause is hanging my app. Does anybody have any ideas on what I could do to figure out what the problem is?

Edit: Yet another addition, this problem only seems to occur when the map is removed from public void onInfoWindowClick(final Marker marker) {. In this function I call the method that removes the view through runOnUiThread().

like image 963
Daniël van den Berg Avatar asked Oct 20 '22 01:10

Daniël van den Berg


1 Answers

Okay, this one took me a while, but I have finally figured it out. It seems to be either a bug or something that isn't documented very well. But apparently calling MapView.onPause() from

@Override
    public void onInfoWindowClick(final Marker marker) {

does not work. This will hang the app. I solved this by doing the following:

@Override
public void onInfoWindowClick(final Marker marker) {
    new Handler().postDelayed(
            new Runnable() {
                @Override
                public void run() {
                    MainActivity.getInstance().runOnUiThread(
                            new Runnable() {
                                @Override
                                public void run() {
                                    myMethods();
                                }
                            });
                }
            }, 1);

and now it works perfectly fine. I'll report this as a bug to Google (https://code.google.com/p/gmaps-api-issues/issues/detail?id=8681).

like image 59
Daniël van den Berg Avatar answered Oct 23 '22 10:10

Daniël van den Berg