Reputation: 87291
I want my Android app work like this:
When a new USB device is connected, and my app is already active, my app asks for permission to use the USB device.
When the same (type of) USB device is connected again, and my app is active, then my app can use it without having to ask for permission again.
If my app is active, the current activity continues running (rather than a new Activity being started) when a known USB device is connected.
I can't get #3 to work: Android 9 insists on starting a new Activity, because my app is registered as a default handler for this USB device. I don't care about default handlers: I don't need an app to be started when the device is connected. (However, I do need #2.)
I tried this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0) {
finish();
return;
}
...
}
Unfortunately this didn't work: it removed both copies of my activity from the stack, navigating back to the Android home screen. I only want to remove or prevent the Activity automatically created by Android when a known USB device is connected.
What I want is possible, the app Serial USB Terminal seems to work like this.
How can I accomplish it?
Upvotes: 2
Views: 1135
Reputation: 437
the 'Serial USB Terminal' app uses
android:launchMode="singleTask"`
As you can see in https://github.com/kai-morich/SimpleUsbTerminal, launchMode=singleTask
and singleTop
have same effect for most apps. Only if you would start another activity in the app, e.g. Android Settings then you should use singleTask, else a new MainActivity would be started when the settings activity is currently shown.
Upvotes: 2
Reputation: 87291
This (singleTop
) seems to have fixed it in the manifest:
<activity android:name=".MainActivity" android:launchMode="singleTop">
I'm not sure if this is the right way to solve it though.
Upvotes: 0