Android launch activity several times by NFC tag

My Android application has 2 actions, the main one for information and one for receiving NFC.

When I launch the application for the first time, I can read NFC tags several times - each time creating a new activity and displaying some information.

If the application is closed, but the phone is connected to the NFC tag, it will show the activity of the nfc tag for the first time, but will never respond to any other tags.

What am I doing wrong?!

Part of the manifest and code for the second action:

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />

<application
android:icon="@drawable/aaa"
android:label="@string/app_name" 
    android:theme="@android:style/Theme.NoTitleBar">

<activity
    android:label="@string/app_name"
    android:name=".MainActivity">
    <intent-filter >
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity 
    android:name=".TagDiscoveredActivity"
    android:screenOrientation="portrait">
    <intent-filter >
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
        <action android:name="android.nfc.action.TAG_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <meta-data
        android:name="android.nfc.action.TECH_DISCOVERED"
        android:resource="@xml/filter_nfc" />
</activity>
</application>

</manifest>    

The code

public class TagDiscoveredActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.info);
        etc
    }

@Override
public void onNewIntent(Intent intent) {
    setIntent(intent);
    resolveIntent(intent);
}

private void resolveIntent(Intent intent) {
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            //| Intent.FLAG_ACTIVITY_SINGLE_TOP);        

    boolean handled = false;

    // Parse the intent
    final String action = intent.getAction();
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) ||
                NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {

        // When a tag is discovered we send it to the service to be save. We
        // include a PendingIntent for the service to call back onto. This
        // will cause this activity to be restarted with onNewIntent(). At
        // that time we read it from the database and view it.
        Parcelable nfctag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (nfctag != null) {
                        //read tag and display here
                    }
                }
            }

    if (!handled) {
        Log.e(logTag, "Unknown intent " + intent);
        finish();
        return;
    }
}

When I launched it and launched the second script - starting directly from NFC without starting the application - the log shows that it works for the first time, but the second time none of the functions writes anything.

Thank you for your helpful suggestions.

+3
2

- , .

, android: launchmode = "singleTask", onNewIntent :

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
+9

, , , , , , . Android ( ), .

+1

All Articles