I am trying to implement an app able to read NFC tags. Let's say I have 5 different activities, then I just want 3 of these activities for being able to read tags. The problem is that I don't know how to 'enable' Act1, Act2 and Act3 for reading, and disable Act4 and Act5.
I guess I have to use, somehow, onNewIntent(), enableForegroundDispatch() in onResume() and disableForegroundDispatch() in onPause() but I don't know where, mainly because I do not really understand the functionality of any of them.
What I actually have is an activity (Act1) whose AndroidManifest's declaration contains the intent-filters, and the activity itself contains the onNewIntent(), enableForegroundDispatch() and disableForegroundDispatch() but, for instance, when I am in Act2 or any other and I read a tag, it goes to Act1 passing through its onCreate() method but it does not goes to onNewIntent(). The only moment when enters in onNewIntent() is when I already am in Act1 and then I read. Actually I want Act2 and Act3 to be able to read but not Act4 and Act5
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nfc);
mPresenter = new NFCPresenter();
mPresenter.attachView(this);
mPresenter.checkAndroidNFCSupport();
setIntent(getIntent());
}
@Override
protected void onPause() {
super.onPause();
mPresenter.mostrarMensaje(CLASS_NAME + " - disableForegroundDispatch");
mPresenter.disableForegroundDispatch();
}
@Override
public void onResume() {
super.onResume();
mPresenter.mostrarMensaje(CLASS_NAME + " - enableForegroundDispatch");
mPresenter.enableForegroundDispatch();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
mPresenter.readNFCTag(intent);
}
public void checkAndroidNFCSupport() {
nfcAdapter = NfcAdapter.getDefaultAdapter(mView);
if (nfcAdapter == null) {
showMessage("NFC Adapter is null.");
mView.finish();
} else {
if (!nfcAdapter.isEnabled()) {
showMessage("Should activate NFC");
} else {
pendingIntent = PendingIntent.getActivity(mView, 0, new Intent(mView, mView.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
showMessage("PendingIntent just created");
}
}
}