【问题标题】:android passing and retrieving extra from activity to fragmentandroid将额外的活动传递和检索到片段
【发布时间】:2013-11-02 22:09:45
【问题描述】:

我有一个活动,用户按下按钮然后发送到片段,但我希望传递一个额外的片段以供片段使用:

活动A(按钮在哪里):

public OnClickListener publish = new OnClickListener(){

       @Override
       public void onClick(View v) {


           Intent intent = new Intent(v.getContext(),ActivityB.class);
           intent.putExtra("friendIdRowID", rowID);
           startActivity(intent);


       }
   };

Activity B 正在加载片段(我希望在其中检索额外的“friendIdRowID”), 片段:

 @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.activity_main2, container, false);

            Bundle extras = getActivity().getIntent().getExtras();

        if (extras != null)
        {

            String myString = extras.getString("friendIdRowID");
}
        }

但它不起作用,我该怎么做才能传递和检索额外的东西?谢谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    您需要使用FragmentsetArguments() 方法将信息传递到您的片段中。在创建片段的活动中,执行以下操作:

    YourFragment f = new YourFragment();
    Bundle args = new Bundle();
    args.putString("friendIDRowID", getIntent().getExtras().getString("friendIDRowID"));
    f.setArguments(args);
    transaction.add(R.id.fragment_container, f, "tag").commit();
    

    然后,覆盖 FragmentonCreate() 方法并执行以下操作:

    Bundle args = getArguments();
    String myString = args.getString("friendIdRowID");
    

    与 Activity 的附加功能一样,您可以根据需要向参数包中添加任意数量的内容。希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多