0

I am trying to display another activity on another display(monitor) in android. Problem occures in scenario when:
From MainActivity I am going to Activity with presentation back and forth, after couple of these, disconnect cable connecting android device to another display application crashes. This error does not occur if I randomly disconnect cable without entering Activity to many times.

Error

06-27 14:35:32.403: E/WindowManager(21837): android.view.WindowLeaked: Activity           com.example.doublescreen.NewActivity has leaked window  com.android.internal.policy.impl.PhoneWindow$DecorView{433b4970 V.E..... R.....I. 0,0- 1280,720} that was originally added here
06-27 14:35:32.403: E/WindowManager(21837):     at com.example.doublescreen.NewActivity.showPresentation(NewActivity.java:48)
06-27 14:35:32.403: E/WindowManager(21837):     at com.example.doublescreen.NewActivity.onCreate(NewActivity.java:39)

Presentation Code:

public class PdfPresentation extends Presentation {
private Context mCtx;
public PdfPresentation(Context outerContext, Display display) {
    super(outerContext, display);
    mCtx = outerContext;
}

protected void onCreate(Bundle savedInstanceState) {
    RelativeLayout layout = new RelativeLayout(mCtx);
    ImageView imgv = new ImageView(mCtx);
    imgv.setBackground(getResources().getDrawable(R.drawable.pic));
    layout.addView(imgv);
    setContentView(layout);
};

}

Activity with Presentation

public class NewActivity extends Activity {
private PdfPresentation mPresentation;
private DisplayManager displayManager;

private final DisplayManager.DisplayListener mDisplayListener =
        new DisplayManager.DisplayListener() {
    @Override
    public void onDisplayAdded(int displayId) {

    }

    @Override
    public void onDisplayChanged(int displayId) {

    }

    @Override
    public void onDisplayRemoved(int displayId) {
        mPresentation.dismiss();
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new);
    displayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
    displayManager.registerDisplayListener(mDisplayListener,null);

    showPresentation();
}

private void showPresentation() {
    Display[] presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
    if (presentationDisplays.length > 0) {
        Display display = presentationDisplays[0];
        mPresentation = new PdfPresentation(this, display);
        mPresentation.show();
    }
}

@Override
protected void onPause() {
    super.onPause();
    displayManager.unregisterDisplayListener(mDisplayListener);
}

}

1 Answers1

0

You need to dismiss() your Presentation at some point, just as you need to dismiss() your Dialog. Right now, you are only calling show().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I am dismissing it in listener when display is being removed – user3783139 Jun 27 '14 at 13:24
  • @user3783139: That should only be called when the display is being removed. It will not be called when the activity exits. You need to `dismiss()` in both cases. That's why I wrapped up a lot of this boilerplate in a `PresentationHelper` class in [my CWAC-Presentation library](https://github.com/commonsguy/cwac-presentation). – CommonsWare Jun 27 '14 at 13:27