0

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");
        }
    }
}
mantc_sdr
  • 451
  • 3
  • 17
  • I need it from more than 1 activity, unless you tell me that rather managing the data in each activity of those activities, I should have just 1 centralised one able to manage the data wherever the origin activity is... – mantc_sdr May 31 '19 at 10:09
  • 1
    No, you can handle NFC events in any of your activities. YOu would simply register for the foreground dispatch in **all** your activities. In those where you don't want to handle NFC events, you would simply ignore the intent that you receive in `onNewIntent()`. In those activities shat should receive NFC events, you would (potentially filter) and process the intent you receive in `onNewIntent()`. That's what why the foreground dispatch system exists after all. – Michael Roland May 31 '19 at 15:27

0 Answers0