简体   繁体   中英

Launching an Activity from intent only if application is not running

My application has an Activity for logging in, LoginActivity and I have defined an intent filter for it so that if someone plugs in a USB device the LoginActivity is launched. This scenario works fine.

The code:

<activity
    android:name=".main.login.LoginActivity"
    ...
    >
  <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>

When the application is running, however, this happens: I'm attaching the USB device and am "redirected" to the login screen ( LoginActivity ). In this scenario I don't want to launch a new activity.

What should I do to get the wanted behavior?

Add this to your activity in AndroidManifest.xml:

android:launchMode="singleTop"

https://developer.android.com/guide/topics/manifest/activity-element.html#lmode

you should put intent filter like as below instead of your code .

            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM