5

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().

Daniël van den Berg
  • 2,197
  • 1
  • 20
  • 45
  • 1
    Could this http://stackoverflow.com/questions/6526874/call-removeview-on-the-childs-parent-first or this http://stackoverflow.com/questions/24271074/removeallviews-error-when-retaining-fragment-instance help you – EugenSunic Sep 29 '15 at 10:32
  • @eugensunic Unfortunately not, I have updated my question with some new findings. – Daniël van den Berg Sep 30 '15 at 08:00
  • 1
    It seems that Google finally, after two and a half years, responded to the bug report. As I'm not working on this project any more, and am not interested in the maps api, I will not be "doing their bidding" by attempting to reproduce the issue in one of their example codes. – Daniël van den Berg May 13 '18 at 20:00

1 Answers1

3

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).

Daniël van den Berg
  • 2,197
  • 1
  • 20
  • 45