0

FRAGMENT 1 :CONTAIN ONLY EDITTEXT FRAGMENT 2 :CONTAIN ONLY EDITTEXT FRAGMENT 3 :CONTAIN BUTTON AND EDITTEXT

fragment are in view pager so,i want to pass value of fragment1 and fragment2 such that when i click on button of third fragment all value from edittext(fragment1 and fragment2) get entered in database.i have already maintain database

Need help with simple example...Thanks in advance

Snofkin Suwal
  • 51
  • 1
  • 6

2 Answers2

0

You can use ViewPager.OnPageChangeListener onPageSelected method.

Add a public method in your fragment like this :

private Object myObject;

public void addObject(Object sampleObject){
    myObject = sampleObject;
}

then in onPageSelected method you can try it with this:

@Override
public void onPageSelected(int position) {
    currentFragment = (Fragment) viewPagerAdapter.instantiateItem(mViewPager, position);

    if (currentFragment instanceof yourFragment ) {

        ((yourFragment ) currentFragment).addObject(objectToBePassed);
    }

}
0

I am not sure but try this method... Use Bundle to send string from one fragment to another

  //Put the value
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);

//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();

Write the code in receiving fragment on createView()

    //Retrieve the value
String value = getArguments().getString("YourKey");
Prabha Karan
  • 1,209
  • 3
  • 15
  • 35