Preventing the start of the main activity, if it is already running

I have an application that starts using the intent-filter action. The problem is that every time an event / action occurs, Android displays a dialog box asking you to start the application, even if it is already running.

I want the behavior to be as follows:

  • The user asked to start the application if the application is not open.
  • The dialog is not displayed if the application is running in the foreground.

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

change

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" /> 
+4
source share
2 answers

You need to set Activity launchMode to singleTask . You can do this by adding an activity attribute to your Android manifest:

 android:launchMode="singleTask" 

See more details.

+9
source

Use the flags when prompting the user to launch the application. Set the flag when it is first turned on, and check each time that this flag is checked. For example if (flag == 1) askusertostart() else continue; if this is clear to you?

0
source

All Articles