Jamie Smith Jamie Smith - 7 months ago 9
Java Question

String doesn't update when passed to different Fragment?

I'm trying to send two strings from Fragment A to Fragment B. Currently, i've implemented an

interface
listener like this...

Fragment A Method:

String arrayTitle1 = arrayList.get(Index).get("ArrayTitle1");
String arrayTitle2 = arrayList.get(Index).get("ArrayTitle2");

((TextFooterListener) getActivity()).onTextFooterListener(arrayTitle1, arrayTitle2);


I pass these strings to the Activity which sends them to Fragment B like this...

Activity:

@Override
public void onTextFooterListener(String arrayTitle1, String arrayTitle2) {

FragmentB.arrayTitle1 = arrayTitle1;
FragmentB.arrayTitle2 = arrayTitle2;

}


Fragment B:

They are then received and stored in Fragment B as public strings.

public String arrayTitle1;
public String arrayTitle2;


And in the
onCreateView
of Fragment B, i try to assign these strings.

arrayTitle1Footer.setText(arrayTitle1);
arrayTitle2Footer.setText(arrayTitle2);


But unfortunately, whenever Fragment A's method gets called, the
onTextFooterListener
doesn't seem to update the strings.

Any suggestions?

Answer

You change the values in Fragment B but never actually reassign the new values into the TextViews.

Change

@Override
public void onTextFooterListener(String arrayTitle1, String arrayTitle2) {
    FragmentB.setTitles(arrayTitle1, arrayTitle2);
}

And in Fragment B :

public void setTitles(String title1, String title2) {
    arrayTitle1 = title1;
    arrayTitle2 = title2;
    arrayTitle1Footer.setText(arrayTitle1);
    arrayTitle2Footer.setText(arrayTitle2);
}
Comments