Mellon
Mellon

Reputation: 38842

Control NFC feature in my app

My app uses only one Activity to host multiple fragments, which means each screen view (page) displayed on the phone is one fragment. The layout of my only activity looks like this:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    > 
     <FrameLayout 
        android:id="@+id/fragment_placeholder"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal|center_vertical"
      /> 
</merge>

As you see above, the <FrameLayout> is the fragment placeholder. So, the 1st fragment is added to the placeholder by:

fragmentTransaction.add(R.id.fragment_placeholder, FirstFragment, TAG1);

The next fragment is shown by replace the existing fragment:

fragmentTransaction.replace(R.id.fragment_placeholder, AnotherFragment, TAG2);

Everything works fine.

Now, I would like to add one new feature to my app, that's to read NFC tag data and show the data on a fragment (NfcFragment).

I followed the Android Developers NFC guide & have successfully configured my app to be able to read NFC data. My AndroidManifest.xml:

<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<application
       …>
        <activity
            android:name="com.nfc.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

            <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:resource="@xml/techs" />

        </activity>
    </application>

Now, when the phone is close to the NFC Tag, my app is launched automatically. But what I need is that, user navigate to the NfcFragment manually by user. On NfcFragment there is a ToggleButton which indicate NFC reader feature ON or OFF. Only when user has selected ON, then if NFC tag is close to phone, the data is read and show on NfcFragment. Otherwise, nothing happen.

How to achieve this feature ? Is it possible to implement this feature with my current architecture of one activity hosting multiple fragments?

Upvotes: 0

Views: 1067

Answers (2)

NFC guy
NFC guy

Reputation: 10228

If you want to dynamically turn on and off the way your app responds to NFC intents, you can use an <activity-alias> section in your manifest with the intent filter for NFC intents. See https://stackoverflow.com/a/11379168/1202968 for the full details.

Upvotes: 1

Morrison Chang
Morrison Chang

Reputation: 12121

Look at BroadcastReceiver to handle the NFC intent filter and decide in the onReceive() if your app will handle it or not. See startActivity() from BroadcastReceiver

Upvotes: 0

Related Questions