How to transfer data from different fragments in tabLayout?


I have a problem passing the selected Item of my ListView to another fragment.

I have a TabbedActivity with 2 tabs. 1st is called OngletCours and the 2nd is called OngletNotes.

I’m getting an error while passing the Item I clicked on.

I have tried the whole weekend but without sucess to transfer the Item I clicked on to the 2nd tab/fragment.

Here is the code from my 1st Fragment/Tab OngletCours (I’m only showing you the setOnItemClickListener :

     l1.setOnItemClickListener(new AdapterView.OnItemClickListener()

        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            OngletNotes fragment = ((Onglets)getActivity()).getOngletNotes();

            if(fragment == null) {
                fragment = OngletNotes.newInstance();
            //récupération de la position convertie en String de l'item que j'ai choisi
            String item = l1.getItemAtPosition(i).toString();

            Bundle args = new Bundle();


            getFragmentManager().beginTransaction().add(, fragment).addToBackStack(null).commit();


My 2nd tab/Fragment OngletNotes looks like this :

public class OngletNotes extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.ongletnotes, container, false);
    //where i want to insert the selectedItem
    TextView Cours = (TextView)rootView.findViewById(;

    Bundle bundle=getArguments();
    String cours="";
    //ERROR !
    cours = bundle.getString("Item");
    //Retrieve the value

    return rootView;

public static OngletNotes newInstance() {
    OngletNotes fragment = new OngletNotes();
    // put values which you want to pass to fragment
    // Bundle args = new Bundle();
    // fragment.setArguments(args);
    return fragment;

I have the following error :

03-06 12:48:13.959 1033-1033/com.example.dasilvadd.students E/AndroidRuntime: FATAL EXCEPTION: main
at com.example.dasilvadd.students.OngletNotes.onCreateView(

Line 23 is the following one :

Bundle bundle=getArguments();

Please help me solving this, I really need to advance in my project. Thank you in advance !


Use shared preferences, create a shared preference in OngletCours then read from in OngletNotes. there is a single instance of this class that all clients share , so in this case it makes sense.Go to this link to refresh it code syntax.

hey just remember this for future purposes, serialize your data whenever your store it. a great library is gson. Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.Just something to think about still.

Answered By – Remario

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published