Activity与Fragment之间传递参数
- - 移动开发 - ITeye博客 在Activity中加载Fragment的时候需要传值到另Fragment,也就是说Activityg与Fragment之间进行参数的传递,可以使用bundle进行参数传递,看代码:. 在activity中获取fragment中的数据. //在activity中获取相对应的fragment中的数据.
public class SaleQuoteActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other_main); //在activity中获取相对应的fragment中的数据 getSupportFragmentManager().beginTransaction().replace(R.id.detail_contain, SaleQuoteFragmentV2.newInstance(getIntent().getStringExtra(ConstantsRoseFashion.KEY_P_ID))).commit(); } //@DebugLog @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); } }
public static SaleQuoteFragmentV2 newInstance(String pid) { //通过Bundle保存数据 Bundle args = new Bundle(); args.putString(ConstantsRoseFashion.KEY_P_ID, pid); SaleQuoteFragmentV2 fragment = new SaleQuoteFragmentV2(); //将Bundle设置为fragment的参数 fragment.setArguments(args); return fragment; }
String pid = getArguments().getString(ConstantsRoseFashion.KEY_P_ID);
Intent intent = new Intent(context, GlobalPriceActivity.class); intent.putExtra(ConstantsRoseFashion.KEY_DATA, data != null ? new Gson().toJson(data) : null); context.startActivity(intent);
String json = getArguments().getString(ConstantsRoseFashion.KEY_DATA); singleGoods = new Gson().fromJson(json, SingleGoods.class);