activity与activity之间传递数据和activity传递数据至fragment

来源:互联网 发布:韦德04年季后赛数据 编辑:程序博客网 时间:2024/05/20 05:10

activity与activity之间传递数据

方法一  Intent intent = new Intent();    Bundle bundle = new Bundle(); //将Bundle 类 用作携带数据    bundle.putString("name", "myname");     bundle.putString("age","18");    intent.putExtras(bundle); //为Intent追加额外的数据    ----------方法二  Intent intent = new Intent();    intent.putExtra("name", "myname");     intent.putExtra("age", "17");    ----------获取值  Bundle bundle=getIntent().getExtras();    String name=bundle.getString("name");    

activity传递数据至fragment


注意: 必须在fragmentTransaction.commit()之前完成Fragment01 fragment = new Fragment01();//创建 bundle 对象        Bundle bundle = new Bundle();        //存取字符串        bundle.putString("str","这是Activity传来的值");        //将bundle 作为参数传递        fragment.setArguments(bundle);        fragmentManager = getFragmentManager();        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();        fragmentTransaction.replace(R.id.fm01id,fragment01);        fragmentTransaction.commit();
fragment中获取到activity所传来的值:String str = (String)getArguments().get(“name”);
0 0
原创粉丝点击