-3

I want to pass a string call custID from one fragement to anopther. but I don't know how to pass it. Below is my code:

class TabsAdapter extends FragmentPagerAdapter {
    public TabsAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public Fragment getItem(int i) {
        switch(i) {
            case 0: return MaterialUpConceptFakePage.newInstance();
            case 1: return MaterialUpConceptFakePage.newInstance();
        }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch(position) {
            case 0: return "Tab 1";
            case 1: return "Tab 2";
        }
        return "";
    }
}

Can someone help me on this?

Reporter
  • 3,897
  • 5
  • 33
  • 47
CM Woo
  • 23
  • 7
  • you can use setArgument() method to pass values to Fragments – playmaker420 Jan 15 '16 at 09:57
  • 7
    Look at the answer to your question [here](http://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments) in detail. – Geeky Singh Jan 15 '16 at 09:59
  • @CM Woo: You might be looking for passing data between fragments of view pager. check this links http://android-er.blogspot.in/2012/06/communication-between-fragments-in.html and http://stackoverflow.com/questions/18492791/how-to-pass-a-string-between-fragments-in-a-viewpager – kevz Jan 15 '16 at 13:11

5 Answers5

0
Bundle bundle=new Bundle();
bundle.putString("ID", "value");
MyFragment obj=new MyFragment ();
obj.setArguments(bundle);
playmaker420
  • 1,527
  • 4
  • 27
  • 52
0

well you can use bundle as below -

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

getFragmentManager()
.beginTransaction()
.replace(R.id.body, fragment, null)
.commit();

and then inside onCreateView() you can receive as below -

Bundle args = getArguments();
int myInt = bundle.getInt(key, defaultValue);
kevz
  • 2,727
  • 14
  • 39
0

User Bundle to pass any value from one Fragment to another. This is how you can pass string from one Fragment to another.

Put the value

SecondFragment secondFragment = new SecondFragment ();
Bundle args = new Bundle();
args.putString("MyKey", "MyValue");
secondFragment.setArguments(args);

Load the second fragment

getFragmentManager().beginTransaction().add(R.id.container, secondFragment).commit();

In onCreateView of the second Fragment:

//Retrieve the value
String value = getArguments().getString("MyKey");
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

While a simple Bundle like other answers works, I can already see you use the newInstance so I will show you an example while still using newInstance to make the code easier to maintain in the future.

In your fragment where you want the String to be add this code

 public static YourFragmentName newInstance(String str) {
        YourFragmentName fragment = new YourFragmentName ();
        Bundle args = new Bundle();
        args.putSerializable("customer_id", str);
        fragment.setArguments(args);
        return fragment;
    }

Then looking at your code instead of

MaterialUpConceptFakePage.newInstance()

you should have

MaterialUpConceptFakePage.newInstance(customerid)

Then inside your onCreateView

String customer_id = (String) getArguments().get("customer_id");
apmartin1991
  • 3,064
  • 1
  • 23
  • 44
0

SharedPreference is a good way for passing data between activities or fragment. You can send any data int,boolean, string int etc.

SharedPreferences preference;
  //Initialize preference

preference = getActivity().getSharedPreferences("STATE",MODE_PRIVATE);
  // it uses key-value pairs
 // put string data to preference
    preference.edit().putString("Your_String_Data", "anything").commit();

//use this to get that value in any fragment.
 preference = getActivity().getSharedPreferences("STATE",
            Context.MODE_PRIVATE);

  String data  = preference.getString("Your_String_Data",null);
Nitesh
  • 318
  • 3
  • 16