简体   繁体   English

NFC前台调度会同时触发onCreate和onNewIntent

[英]NFC foreground dispatch fire both onCreate and onNewIntent

I'm trying to make Activity A(the launcher activity) that can have the below functionality. 我正在尝试使活动A(启动器活动)具有以下功能。

1. Start Activity A with nfc tag and read the nfc tag after the Activity A started. 1.使用nfc标签启动Activity A,并在Activity A启动后读取nfc标签。

2. Detect nfc tag when Activity A is on foreground. 2.当活动A处于前台时,检测nfc标签。

It works at first like for a day. 起初它像一天一样工作。 It will behave like 它会像

onPause -> onNewIntent -> onResume onPause-> onNewIntent-> onResume

After the app is left alone for an day or possible an hour, when I try go to Activity A and scan the nfc tag, it will behave like 将应用程序搁置一天或可能一个小时后,当我尝试转到“活动A”并扫描nfc标签时,其行为将类似于

onPause -> onNewIntent -> onResume -> onPause -> onCreate -> onResume. onPause-> onNewIntent-> onResume-> onPause-> onCreate-> onResume。

Huge problems with the last 3 steps. 最后3个步骤的巨大问题。 I thought it should stop in the onResume after the onNewIntent?? 我认为它应该在onNewIntent之后停止在onResume中?

No matter kill app, clear from android task list, also won't fix the problem until app uninstall and reinstall. 无论是杀死应用程序,还是从Android任务列表中清除,也无法解决问题,直到应用程序卸载并重新安装。 It doesn't happen only on one particular device, it happens on most of my devices. 它不仅发生在特定设备上,而且发生在我的大多数设备上。

Would like to know how to fix this or if there is any work around. 想知道如何解决此问题,或者是否有任何解决方法。 Thanks a lot. 非常感谢。

Below is my code: 下面是我的代码:

Activity A 活动A

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.adapter = NfcAdapter.getDefaultAdapter(this);
    IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    techLists = new String[][] {
            new String[] {
                    NfcF.class.getName()
            }
    };
    intentFiltersArray = new IntentFilter[] { intentFilter };
    this.pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, ((Object) this).getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}

@Override
protected void onPause() {
    if (this.adapter != null) {
        this.adapter.disableForegroundDispatch(this);
    }
    super.onPause();
}

@Override
protected void onResume() {
    if (this.adapter != null) {
        this.adapter.enableForegroundDispatch(this, this.pendingIntent, intentFiltersArray, this.techLists);
    }
    super.onResume();
}

Manifest 表现

<activity
    android:name=".ActivityA"
    android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <meta-data
        android:name="android.nfc.action.TECH_DISCOVERED"
        android:resource="@xml/tech" />
    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
    </intent-filter>
</activity>

can you call this 你可以叫这个吗

super.onResume();

before 之前

if (this.adapter != null) {
        this.adapter.enableForegroundDispatch(this, this.pendingIntent, intentFiltersArray, this.techLists);
    } 

and check again 然后再次检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM