Android 设备旋转期间不扫描 NFC

Android do not scan NFC during device rotation

我有一个使用 NFC 的应用程序。创建 Activity 并调用 enableForegroundDispatch() 后,我的应用程序将扫描并接受 NFC。但是,当 NFC 在旋转期间被扫描时(意味着尚未创建 Activity,并且尚未调用 enableForegroundDispatch()),默认的 Android NFC 扫描器接管显示 "New tag collected" 屏幕。

有什么方法可以在设备旋转期间继续使用 enableForegroundDispatch() 吗?或者有没有办法在设备旋转时暂时 "block" 默认 Android NFC 功能?

谢谢

编辑:应用程序仅在 运行 时才接受 NFC 扫描。在应用未运行时扫描 NFC 运行 仅显示 "New tag collected" 屏幕

有不同的可能性:

为您的activity

设置屏幕方向
<activity android:name=".MyActivity"
          android:screenOrientation="portrait"
          android:label="@string/app_name">

告诉android设备旋转时不重启activityhttps://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

If your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">

更新 1

如果您使用应用android:configChanges。您必须按照文档中的说明自行管理屏幕旋转:

Now, when one of these configurations change, MyActivity does not restart. Instead, the MyActivity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your activity.