0

I'm trying to open my application from NFC app chooser dialog which appears when NFC tag is detected by the system. My app appears in chooser dialog.

Case 1:

When application is running in background. On NFC tag detection bring background activity(top activity in task) to foreground.

Problem

I need to specify which activity handles the nfc intent from system in manifest file. So when user selects my application from app chooser system launchs that specified activity. Instead of bring background activity to the front.

Case 2:

When application is not running lunch application with it's launcher activity.

Problem

To achieve this I need to specify launcher screen as NFC intent handler in manifest file. By doing this my app will fail in case 1!

AndoridManifest.xml snippet

         <activity android:name=".activityName" ...>
                <intent-filter>
                    <action android:name="android.nfc.action.TECH_DISCOVERED"/>
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
                <meta-data
                    android:name="android.nfc.action.TECH_DISCOVERED"
                    android:category="android.intent.category.DEFAULT"
                    android:resource="@xml/nfc_tech_filter" />
         </activity>

What can be the solution to satisfy both cases? I tried with ActivityManager but didn't get the solution with it.

Vijay Vankhede
  • 3,018
  • 1
  • 26
  • 46
  • What do you try ? could you post a part of your AndroidMAnifest contains NFC ? – LaurentY Jun 08 '16 at 08:17
  • @LaurentY I added snippet from AndroidManifest. – Vijay Vankhede Jun 08 '16 at 08:56
  • One solution I think is to create activity with no UI then check if application task. if app is running in background bring second top activity to for ground or launch application! – Vijay Vankhede Jun 08 '16 at 08:58
  • Your tags are specific (from you) or could be any tags on market ? – LaurentY Jun 08 '16 at 09:45
  • Tag is specific for my application. But Ndef record don't have package name of my application. – Vijay Vankhede Jun 08 '16 at 11:10
  • Yes your solution is ok, create an activity to open background or launcher activity , references: http://stackoverflow.com/questions/19452368/catch-nfc-ndef-discovered-intent-from-a-service or http://stackoverflow.com/questions/7819098/android-nfc-start-service – LaurentY Jun 08 '16 at 12:06
  • @LaurentY Thanks you for the references. I found the solution of my problem pls check my answer posted below. – Vijay Vankhede Jun 08 '16 at 12:22
  • Possible duplicate of [Resume the Top Activity instead of starting the Launcher Activity](https://stackoverflow.com/questions/11721619/resume-the-top-activity-instead-of-starting-the-launcher-activity) – Robin Gawenda Jul 03 '18 at 08:28

1 Answers1

0

I solved my problem by adding following code into launcher activity's onCreate(). I kept intent filter and meta data same for launcher activity. The trick here is to check if application task is already running into system or not.

if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
}

Above code will check if application task is already running or not. If its running then launcher activity will close him self and will brought top most activity of task to the front.

Reference:

https://stackoverflow.com/a/21022876/2465752 https://developer.android.com/reference/android/R.styleable.html#AndroidManifestActivity_launchMode

Community
  • 1
  • 1
Vijay Vankhede
  • 3,018
  • 1
  • 26
  • 46