문제

I've developed an application that displays a MapView on the main activity (MapActivity) and this mapview contain overlays which when tapped open a pop-up like window with ViewPager and fragments inside. I've successfully implemented this by calling another activity using FragmentActivity on the pop-up with viewpager but it pauses my MapActivity which I do not want. However, this solution does not cause IntentReceiver leaks.

My other solution was to change MapActivity to FragmentActivity, and calling the pop-up with viewpager works very well. However, when closing the activity triggers the IntentReceiver leaks. Googling around, and found that the solution to this includes unregistering intent receivers on onDestroy method but I didn't register any.

The leaks are NetworkConnectivityListener$ConnectivityBroadcastReceiver & maps.FSTileCache. I'm using mapquest library by the way. And I'm using compatibility libraries also.

Here's the code:

Using MapActivity

public class MyMapActivity extends MapActivity {

public MapView mapView;

protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.my_map_activity); 
        mapView = (MapView) findViewById(R.id.my_map);
        showAllAvailableOverlays();
}

private void showAllAvailableOverlays() {

    Bitmap trafficBitmap = null; 
    List<TrafficProfileDummyModel> dummyTrafficProfiles = new TrafficProfileMockDataSource().getTrafficProfiles();

    for(TrafficProfileDummyModel dtp : dummyTrafficProfiles) {

        View trafficProfileView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.traffic_profile_custom_overlay_item, null);
        ImageView trafficProfileImageView = (ImageView) trafficProfileView.findViewById(R.id.traffic_profile_image_overlay);
        TextView trafficProfileCurrentText = (TextView) trafficProfileView.findViewById(R.id.traffic_profile_custom_overlay_item_currentspeed);
        TextView trafficProfileAverageText = (TextView) trafficProfileView.findViewById(R.id.traffic_profile_custom_overlay_item_averagespeed);

        int curr = (int)Math.round(MyMapActivity.this.currentTripDataModel.getCurrentAverageSpeed()), ave = Integer.parseInt(dtp.getTrafficProfileHistorical()); 

        trafficProfileCurrentText.setText(String.format("%02d",curr));
        trafficProfileAverageText.setText(String.format("%02d",ave));

        Drawable trafficProfileMarkerOverlay = null;
        trafficBitmap=null;
        final GeoPoint geopoint = dtp.getGeopoint(); 

        if((int)Math.round(MyMapActivity.this.currentTripDataModel.getCurrentAverageSpeed()) >= 0 && (int)Math.round(MyMapActivity.this.currentTripDataModel.getCurrentAverageSpeed()) < 35 ) {
            // set marker color to red
            trafficProfileImageView.setImageDrawable(getResources().getDrawable(R.drawable.traffic_profile_red));
            //trafficProfileImageView.setBackground(getResources().getDrawable(R.drawable.traffic_profile_red));
            trafficBitmap = bitmapScalerAndResizer.createDrawableFromView(MyMapActivity.this, trafficProfileView);
            trafficProfileMarkerOverlay = new BitmapDrawable(getResources(),trafficBitmap);

        } else if ((int)Math.round(MyMapActivity.this.currentTripDataModel.getCurrentAverageSpeed()) >= 35 && (int)Math.round(MyMapActivity.this.currentTripDataModel.getCurrentAverageSpeed()) < 49) {
            // set marker color to yellow
            trafficProfileImageView.setImageDrawable(getResources().getDrawable(R.drawable.traffic_profile_yel));
            trafficBitmap = bitmapScalerAndResizer.createDrawableFromView(MyMapActivity.this, trafficProfileView);
            trafficProfileMarkerOverlay = new BitmapDrawable(getResources(),trafficBitmap);

        } else if ((int)Math.round(MyMapActivity.this.currentTripDataModel.getCurrentAverageSpeed()) >= 50) {
            // set marker color to green
            trafficProfileImageView.setImageDrawable(getResources().getDrawable(R.drawable.traffic_profile_green));
            trafficBitmap = bitmapScalerAndResizer.createDrawableFromView(MyMapActivity.this, trafficProfileView);
            trafficProfileMarkerOverlay = new BitmapDrawable(getResources(),trafficBitmap);

        } 

        final DefaultItemizedOverlay overlay = new DefaultItemizedOverlay(trafficProfileMarkerOverlay);

        final OverlayItem overlayItem = new OverlayItem(geopoint, "", "");      
        overlay.addItem(overlayItem);

        overlay.setTapListener(new ItemizedOverlay.OverlayTapListener() {
            @Override
            public void onTap(GeoPoint pt, MapView mapView) {

                // this pauses the  current activity    but doesn't produce intent receiver leaks on activity finish()  
                Intent trafficProfileIntent = new Intent(MyMapActivity.this, TrafficProfilePopUpActivity.class);
                startActivity(trafficProfileIntent);
            }


        });

        mapView.getOverlays().add(overlay);
    }

    trafficBitmap.recycle();
    mapView.invalidate();   
 }
}

TrafficProfilePopUpActivity (the popup window with viewpager & fragments)

public class TrafficProfilePopUpActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.gravity = Gravity.NO_GRAVITY;  
    params.x = 0; params.y = -200;
    getWindow().setAttributes(params);
    setContentView(R.layout.traffic_profile_viewpager_prompt_layout);

    TrafficProfilePagerAdapter pageAdapter = new TrafficProfilePagerAdapter(getSupportFragmentManager(), getFragments());
    ViewPager pager = (ViewPager)findViewById(R.id.myViewPager);
    pager.setAdapter(pageAdapter);

    CirclePageIndicator circlePageIndicator = (CirclePageIndicator) findViewById(R.id.viewPagerIndicator);
    circlePageIndicator.setViewPager(pager,0);
}

private List<Fragment> getFragments(){
    List<Fragment> fList = new ArrayList<Fragment>();
    fList.add(new TrafficProfile_9am_Fragment());
    fList.add(new TrafficProfile_12pm_Fragment());
    fList.add(new TrafficProfile_3pm_Fragment());
    fList.add(new TrafficProfile_6pm_Fragment());
    fList.add(new TrafficProfile_9pm_Fragment());
    return fList;
    }
}

Using FragmentActivity

public class MyMapActivity extends FragmentActivity {

public MapView mapView;

protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.my_map_activity); 
        mapView = (MapView) findViewById(R.id.my_map);
        showAllAvailableOverlays();
}

private void showAllAvailableOverlays() {

    Bitmap trafficBitmap = null; 
    List<TrafficProfileDummyModel> dummyTrafficProfiles = new TrafficProfileMockDataSource().getTrafficProfiles();

    for(TrafficProfileDummyModel dtp : dummyTrafficProfiles) {

        View trafficProfileView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.traffic_profile_custom_overlay_item, null);
        ImageView trafficProfileImageView = (ImageView) trafficProfileView.findViewById(R.id.traffic_profile_image_overlay);
        TextView trafficProfileCurrentText = (TextView) trafficProfileView.findViewById(R.id.traffic_profile_custom_overlay_item_currentspeed);
        TextView trafficProfileAverageText = (TextView) trafficProfileView.findViewById(R.id.traffic_profile_custom_overlay_item_averagespeed);

        int curr = (int)Math.round(MyMapActivity.this.currentTripDataModel.getCurrentAverageSpeed()), ave = Integer.parseInt(dtp.getTrafficProfileHistorical()); 

        trafficProfileCurrentText.setText(String.format("%02d",curr));
        trafficProfileAverageText.setText(String.format("%02d",ave));

        Drawable trafficProfileMarkerOverlay = null;
        trafficBitmap=null;
        final GeoPoint geopoint = dtp.getGeopoint();

        if((int)Math.round(MyMapActivity.this.currentTripDataModel.getCurrentAverageSpeed()) >= 0 && (int)Math.round(MyMapActivity.this.currentTripDataModel.getCurrentAverageSpeed()) < 35 ) {
            // set marker color to red
            trafficProfileImageView.setImageDrawable(getResources().getDrawable(R.drawable.traffic_profile_red));
            //trafficProfileImageView.setBackground(getResources().getDrawable(R.drawable.traffic_profile_red));
            trafficBitmap = bitmapScalerAndResizer.createDrawableFromView(MyMapActivity.this, trafficProfileView);
            trafficProfileMarkerOverlay = new BitmapDrawable(getResources(),trafficBitmap);

        } else if ((int)Math.round(MyMapActivity.this.currentTripDataModel.getCurrentAverageSpeed()) >= 35 && (int)Math.round(MyMapActivity.this.currentTripDataModel.getCurrentAverageSpeed()) < 49) {
            // set marker color to yellow
            trafficProfileImageView.setImageDrawable(getResources().getDrawable(R.drawable.traffic_profile_yel));
            trafficBitmap = bitmapScalerAndResizer.createDrawableFromView(MyMapActivity.this, trafficProfileView);
            trafficProfileMarkerOverlay = new BitmapDrawable(getResources(),trafficBitmap);

        } else if ((int)Math.round(MyMapActivity.this.currentTripDataModel.getCurrentAverageSpeed()) >= 50) {
            // set marker color to green
            trafficProfileImageView.setImageDrawable(getResources().getDrawable(R.drawable.traffic_profile_green));
            trafficBitmap = bitmapScalerAndResizer.createDrawableFromView(MyMapActivity.this, trafficProfileView);
            trafficProfileMarkerOverlay = new BitmapDrawable(getResources(),trafficBitmap);

        } 

        final DefaultItemizedOverlay overlay = new DefaultItemizedOverlay(trafficProfileMarkerOverlay);

        final OverlayItem overlayItem = new OverlayItem(geopoint, "", "");      
        overlay.addItem(overlayItem);

        overlay.setTapListener(new ItemizedOverlay.OverlayTapListener() {
            @Override
            public void onTap(GeoPoint pt, MapView mapView) {

                // this does not pauses the current activity    but when activity closes/finish, produces the intentreceiver leaks              
                final TrafficProfileDialogFragment trafficProfileDialog = new TrafficProfileDialogFragment();
            trafficProfileDialog.show(getSupportFragmentManager(),"");
            }


        });

        mapView.getOverlays().add(overlay);
    }

    trafficBitmap.recycle();
    mapView.invalidate();   
 }
}

TrafficProfileDialogFragment (the popup-like dialog with viewpager & fragments inside)

public class TrafficProfileDialogFragment extends android.support.v4.app.DialogFragment {

 @SuppressWarnings("deprecation")
 @Override
 public Dialog onCreateDialog(Bundle savedInstanceState) {
    final View trafficProfileDialogFragment = this.onCreateView(getActivity().getLayoutInflater(), null, savedInstanceState);
    final Dialog dialog = new Dialog(getActivity(), R.style.DialogCustomTheme);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();  
    lp.dimAmount=0.90f; // Dim level. 0.0 - no dim, 1.0 - completely opaque
    lp.y = 75;
    lp.height = 300;
    lp.width = 300;
    dialog.getWindow().setAttributes(lp);
    dialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
    dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());     
    dialog.setContentView(trafficProfileDialogFragment);
    dialog.setCanceledOnTouchOutside(true);
    return dialog;
 }  

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
    }

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        final View trafficProfileDialogFragment = inflater.inflate(R.layout.traffic_profile_viewpager_prompt_layout, container, false);
        TrafficProfilePagerAdapter pageAdapter = new TrafficProfilePagerAdapter(getChildFragmentManager(), getFragments());
        ViewPager pager = (ViewPager) trafficProfileDialogFragment.findViewById(R.id.myViewPager);
        pager.setAdapter(pageAdapter);

        CirclePageIndicator circlePageIndicator = (CirclePageIndicator) trafficProfileDialogFragment.findViewById(R.id.viewPagerIndicator);
        circlePageIndicator.setViewPager(pager,0);
        return trafficProfileDialogFragment;
 }  


 private List<Fragment> getFragments(){
    List<Fragment> fList = new ArrayList<Fragment>();
    fList.add(new TrafficProfile_9am_Fragment());
    fList.add(new TrafficProfile_12pm_Fragment());
    fList.add(new TrafficProfile_3pm_Fragment());
    fList.add(new TrafficProfile_6pm_Fragment());
    fList.add(new TrafficProfile_9pm_Fragment());    
    return fList;
    }    
}

How can I resolve this issue?

도움이 되었습니까?

해결책

I've managed to somehow solved the issue by calling mapView.destroy() on the Activity's onDestroy() method. Many thanks guys.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top