How to transfer data between actions in a fragment using intent in Android

I can save the value in one variable, now I want to pass this variable to a fragment.

With the following code, I can upload fragments.

   public class AndroidListFragmentActivity extends Activity {
Fragment2 f2;
public static String itemname;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.apetiserfragement);
    itemname=getIntent().getStringExtra("itemname");
    Bundle args=new Bundle();
    args.putString("itemname", itemname);
    f2=new Fragment2();
    f2.setArguments(args);

}
}

(Here I load the snippet using the XML page) itemname

The output is divided into 2 windows, one for extension for listfragment (for list). One for fragments

Fragment2.xml

    public class Fragment2 extends Fragment {
String itemname;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    System.out.println(getArguments().getString("itemname"));

    return inflater.inflate(R.layout.fragment2, container, false);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

}

  }

AndroidListFragmentActivity in this class itemname I need to pass Fragment2.class.

+5
source share
2 answers

Use the package contained in one of the fragment constructors, and then you can get the bundle by doing getArguments()to the right where you want inonCreateView()

Example:

YourFragment.instantiate(context,"fragName",yourBundleWithValue);

Then in your snippet:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    getArguments().getStringValue("name"); //Returns your string value

    return inflater.inflate(R.layout.fragment2, container, false);
}
+5

FragmentManager Fragment.

-1

All Articles