我有两个活动 A 和 B。A 总是在 B 和 B 使用对话主题之前开始(这是必需且重要的)。两者都使用 NFC 前台调度机制,效果很好。但是,当方向改变时,就会出现问题。我应该在 onResume 和 onPause 方法中启用和禁用前台调度。
怎么了:
- A 启动,A onResume 被调用
- A onPause,B 启动,B onResume 被调用(一切都按预期工作)
- 方向改变
- B onPause 被调用,B 再次启动,B onResume 被调用,A 再次在后台启动,A onResume 被调用,A onPause 被调用
--> 现在 B 的前台调度不再起作用了。
B 的对话主题导致了这个问题,因为一切都适用于正常主题。有解决方法吗?
解决方案:
感谢您的评论,但我找到了一个解决方法,这会有所帮助。在活动 BI 的 onCreate 方法中执行以下操作:
/*
* This workaround is needed as this activity uses the dialog theme. When the background activity uses the foreground
* dispatch as well, then it breaks this activity's foreground dispatch on an orientation change.
*/
mCancelButton.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
NfcForegroundDispatch.setupForegroundDispatch(NfcDetectorActivity.this);
}
});
正如我所说,这是一种解决方法而不是解决方案。