문제

I have an application that is launched via an intent-filter action. The problem is that every time the event/ action occurs, Android displays a dialog asking to launch the app even if it is already started.

I want the behavior to be as follows:

  • User asked to launch the app if app is not open.
  • Dialog does not display if app is running in the foreground.

Is there a way to achieve both of these goals? I am targeting Android 4.0.

edit

Here is my intent filter:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>

       <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
        </intent-filter>

       <meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
            android:resource="@xml/device_filter" />
도움이 되었습니까?

해결책

You need to set the Activity's launchMode to singleTask. You can do this by adding the activity attribute in your Android Manifest:

android:launchMode="singleTask"

See the documentation for more details.

다른 팁

Use flags when asking the user to start the application. Set the flag when this is first enabled and each time check if this flag is ticked. For example if (flag == 1) askusertostart() else continue; if this is clear to you?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top