如何将Activity
的值传递给Fragment
?当从活动传递值到片段时获得空值?
据我所知,我可以在Activity
和getArgument
的Fragment
中使用setArgument
。但我没有运气。该值仍然返回为空。
活动
public class nfc_activity extends Activity {
private ImageView mCardView;
MyFragmentA f2;
public static String itemname;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button goButton = (Button)findViewById(R.id.go);
goButton.setOnClickListener(mGoListener);
}
private OnClickListener mGoListener = new OnClickListener()
{
public void onClick(View v)
{
// Here we start up the main entry point of our redirection
// example.
String itemname ="1";
MyFragmentA fragment = new MyFragmentA();
Bundle bundle2 = new Bundle();
bundle2.putString("key", itemname);
fragment.setArguments(bundle2);
}
};
}
片段
public class MyFragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle = this.getArguments();
if (bundle != null) {
String hello = bundle.getString("key", defaultValue);
System.out.println(hello);
}
[如何在片段之间传递数据](http://stackoverflow.com/questions/5194548/how-to-pass-data-between-fragments) – prolink007 2012-07-31 17:18:13
您提到的API级别是什么? – Nerd 2012-07-31 17:25:34