我有带有两个不同片段(A,B)的Activity1。如何在片段B可见时禁用NFC标签阅读器,而在片段A可见时如何启用它?
我想通过在enableForegroundDispatch()
内调用disableForegroundDispatch()
内的enableForegroundDispatch
和Activity1.onAttachFragments()
内的disableForegroundDispatch
来附加B时在活动上使用Activity1.onBackPressed
和history = model.fit_generator(generator=train_generator,
steps_per_epoch=math.ceil(n_train_samples/batch_size),
epochs=epochs,
callbacks=[tf.keras.callbacks.ModelCheckpoint('tinydsod300_weights_epoch--{epoch:02d}_loss--{loss:.4f}_val_loss--{val_loss:.4f}.h5',
monitor='val_loss',
verbose=1,
save_best_only=True,
save_weights_only=True,
mode='auto', period=1),
tf.keras.callbacks.LearningRateScheduler(lr_schedule),
tf.keras.callbacks.EarlyStopping(monitor='val_loss',
min_delta=0.001,
patience=2),
tf.keras.callbacks.TerminateOnNaN(),
tf.keras.callbacks.TensorBoard(log_dir='./logs'),
tf.keras.callbacks.BaseLogger()],
validation_data=val_generator,
validation_steps=math.ceil(n_val_samples/batch_size)
但我想这行不通,因为该活动始终在前景中可见,那么它将始终收到标签意图...
答案 0 :(得分:0)
由于没有人回答这个问题,我将分享我自己的似乎对我有用的解决方案,即使代码根本不“优雅”。首先,我在 MainActivity 中添加了一个简单的布尔值 isNfcActive
,并在我的 ReaderFragment 中进行了更改,如下所示:
override fun onResume() {
super.onResume()
val activity = activity as MainActivity
activity.isNfcActive = true
}
override fun onDestroy() {
super.onDestroy()
val activity = activity as MainActivity
activity.isNfcActive = false
}
现在,每次我从 NFC 卡获得新意图时,我都会检查读取是否处于活动状态。这样,除了阅读器手机之外的每个片段都会振动,但只有在您想要的片段中,才会处理此意图:
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (intent != null && isNfcActive) {
processIntent(intent)
}
}