如何在Fragment中使用onNewIntent(Intent intent)方法? [英] How to use method onNewIntent(Intent intent) inside a Fragment?

查看:1690
本文介绍了如何在Fragment中使用onNewIntent(Intent intent)方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从设备中使用NFC硬件.但是,问题在于,当我注册活动以接收意图时:

I'm trying to use NFC Hardware from my device. But, the problem is that when I register the Activity to receive the Intent:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

我在活动而不是片段中收到结果.有没有办法在Fragment中处理此结果?

I receive the result in an Activity instead of a Fragment. Is there a way to handle this result inside a Fragment?

提前谢谢!

推荐答案

onNewIntent属于Activity,因此您无法在其片段中使用它.您可以做的是在数据到达onNewIntent时将数据传递给片段,前提是您具有对该片段的引用.

onNewIntent belongs to Activity so you cannot have it in your fragment. What you can do is pass the data to your fragment when it arrives in onNewIntent provided you have the reference to the 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());
    }

}

这篇关于如何在Fragment中使用onNewIntent(Intent intent)方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆