Miche
Miche

Reputation: 23

Check if the app has been started by a NFC tag

There is a way to understand if the app has been started by a NFC tag?

I have programmed a nfc tag for launching my app writing the app package name (with a third app) and i want to start a specific fragment in that case, how can i do that?

Upvotes: 1

Views: 1044

Answers (1)

Ridcully
Ridcully

Reputation: 23655

You have to add an intent filter to an activity of your app, which registers that activity to be started by an nfc event. There are different ways to specify that filter, based on mime-type or technology etc. Then in that activity, you can check in onNewIntent() and in onResume() if the intent that started the activity was an nfc intent.

public void onNewIntent(Intent newIntent) {
    setIntent(newIntent);
    // onResume is called afterwards, so we handle intent there
}

public void onResume() {
    Intent intent = getIntent();
    if (intent != null && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Toast.makeText("NFC!", Toast.LENGTH_LONG).show();
        // Here you could start your Fragment
    }
}

Please check the official documentation about the options you have for intent filters etc.

https://developer.android.com/guide/topics/connectivity/nfc/nfc.html

Upvotes: 1

Related Questions