I have 2 activities within my app. Say FirstActivity
and SecondActivity
. FirstActivity
is the MAIN and LAUNCHER activity. SecondActivity
uses usb devices. I want the prompt for USB permission to appear only once within lifetime of the app.
If there was only one Activity, the following lines in manifest solved my purpose:
<activity android:name=".FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<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/usb_device_filter" />
</activity>
This was doing the following:
- Launching FirstActivity whenever usb device (mentioned in xml) is connected is app is not already open.
- Prompt for usb device permission appears only once.
How do I modify this to achieve the following:
If SecondActivity is already running and a new usb device is attached, I must be able to use the device without relaunching the app. So I have written a broadcast receiver for the same as follows:
public class TriggerReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { read connected usb devices and register serial port call listener back. } }
But the problem is FirstActivity
gets relaunched again when usb device is attached while SecondActivity
is running. How do I avoid this ?
Will add more information if needed. Would be thankful for any help.