0

So I got 2 fragments and an activity. Those 2 fragments are communicating each other via an interface. Problem is, I can't change the TextView in the second fragment from Activity wih some text from fragment1.

There is the same problem( How to change fragment's textView's text from activity ) but there is no real solution so i thought i should open this.

Fragment 1:

public class test_layout extends Fragment {

DearListener activityCommander;
TextView custom_question_bar, list_number;
String selectedCustomQuestion;
boolean buttonClicked;
Button yiyecekButton;
Context c;

public interface DearListener {
    public void getMyText(String theQuestion);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        activityCommander = (DearListener) activity;
        Log.i("MYLOG", "onAttach");
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString());
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.new_test_layout, container, false);
    //custom_question_bar= (TextView) v.findViewById(R.id.custom_question_bar);
    //list_number= (TextView) v.findViewById(R.id.list_number);
    yiyecekButton = (Button) v.findViewById(R.id.yiyecekButton);
    Log.i("MYLOG", "Button referenced");
    yiyecekButton.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v){
                    Log.i("MYLOG", "onCLickListener Called");
                    onClickYiyecek(v);

                }
    }
    );
    return v;
}

public void onClickYiyecek(View v){
    Log.i("MYLOG", "OnClickYiyecek");
    activityCommander.getMyText("En sevdiğim yiyecek ne?");
}

public void onClickQuestionReady(View v){
    if( custom_question_bar == null)
    {
        Toast.makeText(c, "Boşluğu doldurmadınız", Toast.LENGTH_SHORT).show();
    }
    activityCommander.getMyText(custom_question_bar.getText().toString());
}

}

And the Activity:

public class NewTest extends Activity implements test_layout.DearListener {

FragmentManager manager;
test_layout test_layout_frag;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_holder);
    manager=getFragmentManager();
    test_layout_frag = new test_layout();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.the_frame_layout, test_layout_frag);
    transaction.commit();
}
@Override
    public void getMyText(String theQuestion) {
        manager=getFragmentManager();
        answer_layout answer_layout_frag = new answer_layout();
        answer_layout answer_layout_obj = (answer_layout)getFragmentManager().findFragmentById(R.id.fragment2);
        answer_layout_obj.changeText(theQuestion);
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.the_frame_layout, answer_layout_frag);
        transaction.commit();
    Log.i("MYLOG", "Transaction Called");
    //transaction.addToBackStack(null);
}

}

And the second fragment:

public class answer_layout extends Fragment {
TextView yourSelectedQuestion;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.new_answer_layout, container, false);
    yourSelectedQuestion= (TextView) v.findViewById(R.id.yourSelectedQuestion);
    return v;
}
public void changeText(String a) {
    Log.i("MYLOG", "changeText");
    Log.i("MYLOG", a);
    yourSelectedQuestion.setText(a);
}

}

No exception is given but the TextView in second fragment isn't changing.

Since i didn't write the xml here, I can if its needed. Using FrameLayout and it contains 2 fragments inside it.

Community
  • 1
  • 1
Tuna Yagci
  • 300
  • 1
  • 9

1 Answers1

0

In myFragment class write a static method:

public static myFragment newInstance(String txt) {
        myFragment myfragment = new myFragment();

        Bundle args = new Bundle();
        args.putString("TEXT", txt);
        myfragment.setArguments(args);

        return myfragment;
    }

after that in OnCreate method of myFragment class inflate the textView tv and

tv.setText(getArguments().getString("TEXT"));

In main FragmentActivity class do:

myFragment fragment = myFragment.newInstance(textToBeSent);

before replace,

fragmentTransaction.replace(R.id.fr_dynamic, fragment);
fragmentTransaction.commit();
Jyo the Whiff
  • 829
  • 9
  • 23