The best approach is to use to use the set of commands related to DisplayID, that allows you to listen for displays to be added, changed or removed.
Here is a quick example how it goes to change your display/HDMI:
private final DisplayManager.DisplayListener mDisplayListener =
new DisplayManager.DisplayListener() {
@Override
public void onDisplayAdded(int displayId) {
Log.d(TAG, "Display #" + displayId + " added.");
mDisplayListAdapter.updateContents();
}
@Override
public void onDisplayChanged(int displayId) {
Log.d(TAG, "Display #" + displayId + " changed.");
mDisplayListAdapter.updateContents();
}
@Override
public void onDisplayRemoved(int displayId) {
Log.d(TAG, "Display #" + displayId + " removed.");
mDisplayListAdapter.updateContents();
}
};
And here how to get all your HDMI/display devices available to connect:
protected void onResume() {
// Be sure to call the super class.
super.onResume();
// Update our list of displays on resume.
mDisplayListAdapter.updateContents();
// Restore presentations from before the activity was paused.
final int numDisplays = mDisplayListAdapter.getCount();
for (int i = 0; i < numDisplays; i++) {
final Display display = mDisplayListAdapter.getItem(i);
final PresentationContents contents =
mSavedPresentationContents.get(display.getDisplayId());
if (contents != null) {
showPresentation(display, contents);
}
}
mSavedPresentationContents.clear();
// Register to receive events from the display manager.
mDisplayManager.registerDisplayListener(mDisplayListener, null);
}
To unregister you use:
//unregisterDisplayListener(DisplayManager.DisplayListener);
@Override
protected void onPause() {
// Be sure to call the super class.
super.onPause();
// Unregister from the display manager.
mDisplayManager.unregisterDisplayListener(mDisplayListener);
// Dismiss all of our presentations but remember their contents.
Log.d(TAG, "Activity is being paused. Dismissing all active presentation.");
for (int i = 0; i < mActivePresentations.size(); i++) {
DemoPresentation presentation = mActivePresentations.valueAt(i);
int displayId = mActivePresentations.keyAt(i);
mSavedPresentationContents.put(displayId, presentation.mContents);
presentation.dismiss();
}
mActivePresentations.clear();
}
About "invalidate" HDMI output, if it eventually occurs, just redraw it. This should solve any "invalidate", in case it happens.
You might find useful to check further documentation.