1

i must pass two parameters from fragment to another fragment. First parameter is type String and second parameter is type int. i want pass parameters with bundle but doesn't work.

FIRST FRAGMENT

Fragment fragment = new Fragment();
                Bundle bundle = new Bundle();
                bundle.putInt("totale", totalAmount);
                fragment.setArguments(bundle);

SECOND FRAGMENT

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        TextView titolo2 = (TextView) getView().findViewById(R.id.quantità2);
        Bundle bundle=this.getArguments();
        int myInt = bundle.getInt("totale", 0);
        titolo2.setText(myInt);

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




    }
Markella92
  • 71
  • 2
  • 13
  • 1
    returning layout for Fragment after calling `getView()` to access TextView from layout probably causing issue and also `setText ` take `CharSequence` as parameter instead of `int` so use `String.valueOf` to show `int` in TextView – ρяσѕρєя K May 04 '16 at 09:44
  • if i return fragment layout after getView(), Bundle bundle=this.getArguments(); is underline in red by IDE – Markella92 May 04 '16 at 09:49
  • see http://alvinalexander.com/source-code/android/android-example-oncreateview-method-fragment-class example – ρяσѕρєя K May 04 '16 at 09:50
  • First Fragemnt: `Bundle bundle = new Bundle(); bundle.putInt("message", "" + totalAmount); FragmentManager fragmentManager = getFragmentManager(); Fragment fragment = new Fragment(); fragment.setArguments(bundle);` Second Fragment: `int aa = getArguments().getInt("message");` – User Learning May 04 '16 at 09:50
  • you will get the answere ? – nakul May 04 '16 at 10:12
  • you are trying to get the data while creating the view try it in the oncreate function – nakul May 04 '16 at 10:17
  • i make this: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_two, container, false); TextView titolo2 = (TextView)rootView.findViewById(R.id.quantità2); Bundle bundle=getArguments(); int myInt = bundle.getInt("totale", 0); titolo2.setText(myInt); return rootView; } – Markella92 May 04 '16 at 10:24

5 Answers5

0

In first fragment write this:

TwoFragment fragment = new TwoFragment ();
                    Bundle bundle = new Bundle();
                    bundle.putInt("totale", totalAmount);
                    fragment.setArguments(bundle);

Second fragmnent:

Bundle bundle=getArguments();
        int myInt = bundle.getInt("totale", 0);
        titolo2.setText(myInt);
Neha Tyagi
  • 3,821
  • 3
  • 15
  • 25
0

Change Second Fragment like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_two, container, false);

    TextView titolo2 = rootView.findViewById(R.id.quantità2);
    Bundle bundle=this.getArguments();
    int myInt = bundle.getInt("totale", 0);
    titolo2.setText(myInt + "");

    return rootView;
}
manao
  • 328
  • 5
  • 12
  • It should work then. Did it throws any exceptions or bundle doesn't have totale key? – manao May 04 '16 at 10:29
  • THIS IS MY BOUNDLE:Fragment fragment = new Fragment(); Bundle bundle = new Bundle(); bundle.putInt("totale", totalAmount); fragment.setArguments(bundle); – Markella92 May 04 '16 at 11:08
0

You should do the work of second fragment as:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_two, container, false);
}


public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    TextView titolo2 = view.findViewById(R.id.quantità2);
    Bundle bundle=getArguments();
    int myInt = bundle.getInt("totale", 0);
    titolo2.setText(String.valueOf(myInt));
}
Hassan Jamil
  • 951
  • 1
  • 13
  • 33
  • Did you instantiate second fragment's class instance as SecondFragmentClass secFragment = new SecondFragmentClass(); and then gave your bundle to the instance as secFragment.setArguments(bundle); ? – Hassan Jamil May 05 '16 at 09:41
0

Create a public class with two static members like this :

public class SomeClass {
    public static String stringValue="";
    public static int integerValue=0;
}

Then from your first fragment just set values like this :

SomeClass.stringValue="yourValue";
SomeClass.integerValue=yourIntegerValue;

You can use these variable from your second fragment where you want :

int a =SomeClass.integerValue;
String str=SomeClass.stringValue;
Akhil Soman
  • 2,077
  • 2
  • 17
  • 30
0

If both fragments belong to the same activity, better to use this approach:

In activity you should save references to fragments, and all comunications between this fragments should be through activity, let's name it BaseActivity. For example add to activity method which will pass params to second fragment:

void updateSecondFragment(int param1, String param2){
    mFragment2.updateTitle(param2);
}

In second fragment:

void updateTitle(String title){
    mTextView.setText(title);
}

And in first fragment:

BaseActivity mActivity;

@Override
void onAttach(Activity activity) {
    if (activity instanceOf BaseActivity){
        mActivity = (BaseActivity) activity;
    }
}

private void updateSecondFragment(){
    if (mActivity != null){
        mActivity.updateSecondFragment(int param1, String param2);
    }
}
Denys Vasylenko
  • 2,135
  • 18
  • 20