androidandroid-fragmentstabsargumentsextras

How to send/receive extras to Fragments in SwipeView Activity


I am trying to use the Swipe View with Tabs in Android and i need to pass some variables to the fragments of the tabs to show a customized content, my problem is that I always have the bundle to 0 so I think I am doing something wrong with the sending of the extras from the fragment container page. this is the code of the fragment and of the main fragment page containing the tabs.

public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class
        // below).

        prof = getIntent().getIntExtra("prof", 0);
        id = getIntent().getIntExtra("id", 0);
        bund = new Bundle();
        bund.putInt("prof", prof);
        bund.putInt("id", id);

        switch (position) {
            case 0:
                // Text fragment activity
                FragmentOne fragment = new FragmentOne();
                fragment.setArguments(bund);
                return fragment;
            case 1:
                // Gallery fragment activity
                FragmentTwo fragment2 = new FragmentTwo();
                fragment2.setArguments(bund);
                return fragment2;
            case 2:
                 //Audio fragment
                FragmentThree fragment3 = new FragmentThree();
                fragment3.setArguments(bund);
                return fragment3;
            }

            return null;
    }

while the fragment code is placed in the onCreateView method

Bundle bundle = getArguments();
    if (bundle!=null){ 
    prof = bundle.getInt("prof");
     id = bundle.getInt("id");
    }

I can read the arguments coming into the fragment container but into the fragment I can't read them, what did I forget?


Solution

  • You can try with SharedPreference instead of Bundle here :

    Instantiate SharedPreference in your FragmentStatePagerAdapter :

    SharedPreference mPref = mContext.getSharedPreferences("myPreference",
                Context.MODE_PRIVATE);
    

    Store prof and id to mPref :

    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class
        // below).
    
        prof = getIntent().getIntExtra("prof", 0);
        id = getIntent().getIntExtra("id", 0);
        // store value in mPref
        mPref.putInt("prof", prof);
        mPref.putInt("id", id);
    
        // commit changes
        mPref.commit();
    
    switch (position) {
        case 0:
            // Text fragment activity
            FragmentOne fragment = new FragmentOne();
    
            return fragment;
        case 1:
            // Gallery fragment activity
            FragmentTwo fragment2 = new FragmentTwo();
    
            return fragment2;
        case 2:
             //Audio fragment
            FragmentThree fragment3 = new FragmentThree();
    
            return fragment3;
        }
    
        return null;
    }
    

    Retrive values inside onCreateView() of any Fragment :

    SharedPreference mPrefFrag = getActivity().getSharedPreferences("myPreference",
            Context.MODE_PRIVATE);    
    int prof = mPrefFrag.getInt("prof",0);
    int id = mPrefFrag.getInt("id",0);