意图传递到我的片段 [英] Pass intent to my fragment

查看:299
本文介绍了意图传递到我的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现在 NFC前景调度在我的活动。在code正常工作,当NFC标签亲近我的手机, onNewIntent(意向意图)被调用。

I have implemented the NFC foreground dispatch in my Activity. The code works fine, when the NFC tag get close to my phone, the onNewIntent(Intent intent) is called.

现在,我想表现出的片段(MyFragment.java)的 onNewIntent(意向意图)时调用。有在片段类中定义的 resolveIntent(意向意图)方法。

Now, I would like to show an Fragment(MyFragment.java) when the onNewIntent(Intent intent) is invoked. There is resolveIntent(Intent intent) method defined in the Fragment class.

下面是我的活动的一些code

public class MainActivity extends Activity{
   …
   @Override
   public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

      FragmentManager fragmentManager = activity.getFragmentManager();
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

      //How to pass the 'intent' to MyFragment?
      MyFragment fragment = new MyFragment();

    fragmentTransaction.replace(R.id.placeholder, fragment, fragmentName);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
   }        
}

$ C $的ç MyFragment.java

public class MyFragment extends Fragment{
    …
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
       ...
    }
    @Override
    public void onResume(){
      super.onResume();

       //How to get 'intent' here 
       resolveIntent(intent);
    }

    private void resolveIntent(Intent intent){
       ...
    }
}

我的问题是我怎么能传递意图 onNewIntent(意向意图) MyFragment ,这样我可以处理的意图 MyFragment onResume()

My question is how could I pass the intent from onNewIntent(Intent intent) to MyFragment so that I can handle the intent in MyFragment in onResume()?

推荐答案

我想通了,意图是parcelable对象,我可以使用bundle.putParcelable()方法的意图实例传递到下一个片段。

I figured out that Intent is an parcelable object, I can use bundle.putParcelable() method to pass the intent instance to the next fragment.

这篇关于意图传递到我的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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