0

我正在嘗試使Activity A(啓動器活動)具有以下功能。NFC前臺調度同時觸發onCreate和onNewIntent

1.使用nfc標籤啓動活動A並在活動A啓動後讀取nfc標籤。

2.活動A在前臺時檢測nfc標籤。

它首先適用於一天。它會像

的onPause - > onNewIntent - >的onResume

應用程序被單獨留在白天或可能一個小時後,當我嘗試去到活動A和掃描NFC標籤,它會像

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

最後3個步驟的巨大問題。我認爲它應該停止onNeumeIntent後的onResume?

無論是殺應用,從android任務清單中清除,也不會修復問題,直到應用程序卸載並重新安裝。它不會發生在一個特定的設備上,它發生在我的大多數設備上。

想知道如何解決這個問題,或者如果有任何解決方法。非常感謝。

下面是我的代碼:

活動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(); 
} 

清單

<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> 
+0

是'的onDestroy()'被前'的onCreate()'叫什麼名字?如果是這樣,您可能會看到對配置更改的響應。 Android正在殺死當前的Activity,然後創建一個新的來處理配置更改。您可以通過在清單中的''標籤中添加'android:configChanges =「orientation | screenSize」'來防止這種情況。 –

回答

1

,你可以調用這個

super.onResume(); 

之前

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

,並再次檢查

+0

看起來它現在不再發生。將繼續觀察行爲。謝謝。 – DonutKing

相關問題