【问题标题】:Android NFC and onNewIntent()Android NFC 和 onNewIntent()
【发布时间】:2017-07-28 13:11:06
【问题描述】:

在 NFC Activity 中编写 onNewIntent(Intent intent) 方法时,调用 super.onNewIntent(intent) 是否必要/正确?

我问是因为这个official example 包含它:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    ...
    if (intent != null && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Parcelable[] rawMessages =
            intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMessages != null) {
            NdefMessage[] messages = new NdefMessage[rawMessages.length];
            for (int i = 0; i < rawMessages.length; i++) {
                messages[i] = (NdefMessage) rawMessages[i];
            }
            // Process the messages array.
            ...
        }
    }
}

但是这个other official example 没有:

public void onNewIntent(Intent intent) {
    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    //do something with tagFromIntent
}

大概,第二个例子不完整(而且不正确),但我想确定一下。

【问题讨论】:

    标签: android android-intent nfc


    【解决方案1】:

    根据官方documentation,没有理由致电super.onNewIntent(..)

    例如,您可以检查onDestroy() 方法documentation。它包括下一行:

    派生类必须调用超类的实现 这种方法。否则,将引发异常。

    【讨论】:

    • 谢谢。调用它会有什么坏处吗?
    • @ban-geoengineering 不。只有 1 行无用的代码。
    • @ban 如果这能解决您的问题,请批准答案。提前致谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多