我正在尝试通过我的设备使用 NFC 硬件。但是,问题是当我注册 Activity 以接收 Intent 时:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
我在 Activity 而不是 Fragment 中收到结果。有没有办法在 Fragment 中处理这个结果?
提前致谢!
最佳答案
onNewIntent
属于 Activity,因此您不能将其包含在 fragment 中。你可以做的是在数据到达 onNewIntent
时将数据传递给你的 fragment ,前提是你有 fragment 的引用。
Fragment fragment;
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Check if the fragment is an instance of the right fragment
if (fragment instanceof MyNFCFragment) {
MyNFCFragment my = (MyNFCFragment) fragment;
// Pass intent or its data to the fragment's method
my.processNFC(intent.getStringExtra());
}
}
关于android - 如何在 fragment 中使用 onNewIntent(Intent intent) 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30043326/