如何将值从 a 传递Activity
到 a Fragment
?
我知道我可以setArgument
在 myActivity
和getArgument
in 中使用Fragment
。但我没有运气。该值仍返回为 null。
活动
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);
}