如何將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