package com.example.jackytsang.presentationapi;

import android.annotation.TargetApi;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;

import java.lang.annotation.Target;

public class MainActivity extends AppCompatActivity {

    MyPresentation mPresentation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_primary);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
            multiInit();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void multiInit() {
        DisplayManager dm = (DisplayManager) getSystemService(DISPLAY_SERVICE);
        if (dm == null)
            return;

        Display[] displays = dm.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);

        for (Display display : displays) {
            mPresentation = new MyPresentation(this, display);
            mPresentation.show();
        }
    }

}

@@ -353,16 +353,24 @@
  %7B%0A%0A    
+private 
 MyPresen
@@ -390,16 +390,64 @@
 ntation;
+%0A    private MyDisplayListener mDisplayListener;
 %0A%0A    @O
@@ -712,16 +712,199 @@
 %0A    %7D%0A%0A
+    @Override%0A    protected void onDestroy() %7B%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.JELLY_BEAN_MR1)%0A            multiDestroy();%0A        super.onDestroy();%0A    %7D%0A%0A%0A
     @Tar
@@ -1102,16 +1102,128 @@
 eturn;%0A%0A
+        mDisplayListener = new MyDisplayListener();%0A        dm.registerDisplayListener(mDisplayListener, null);%0A
         
@@ -1463,10 +1463,1219 @@
 %0A    %7D%0A%0A
+    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)%0A    private void multiDestroy() %7B%0A        if (mDisplayListener != null) %7B%0A            DisplayManager dm = (DisplayManager) getSystemService(DISPLAY_SERVICE);%0A            dm.unregisterDisplayListener(mDisplayListener);%0A            mDisplayListener = null;%0A        %7D%0A    %7D%0A%0A    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)%0A    private class MyDisplayListener implements DisplayManager.DisplayListener %7B%0A%0A        @Override%0A        public void onDisplayAdded(int displayId) %7B%0A            DisplayManager dm = (DisplayManager) getSystemService(DISPLAY_SERVICE);%0A%0A            Display display = dm.getDisplay(displayId);%0A            if (display == null)%0A                return;%0A%0A            mPresentation = new MyPresentation(MainActivity.this, display);%0A            mPresentation.show();%0A        %7D%0A%0A        @Override%0A        public void onDisplayRemoved(int displayId) %7B%0A            if (mPresentation != null && mPresentation.getDisplay().getDisplayId() == displayId) %7B%0A                mPresentation.dismiss();%0A                mPresentation = null;%0A            %7D%0A        %7D%0A%0A        @Override%0A        public void onDisplayChanged(int displayId) %7B%0A%0A        %7D%0A    %7D%0A
 %7D%0A

add a listener to listen add, remove and remove of a screen, https://blog.stylingandroid.com/multiple-displays-part-4/