I know that I can use USB host mode with this manifest configuration:
<activity
android:name="com.mypackage.MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
And this will launch my activity every time a USB device is connected. The problem is that I don't want it to reopen if it is already running in foreground. I also don't want it to start if any other of the activities of my app is already running in foreground. How to do that?