Skip to content Skip to sidebar Skip to footer

How Can I Update A Textview From One Fragment To Another

I have an activity 'MainActivity' that contains two fragments. Fragment1 has a EditText and a button. When the user presses the button the text inside the EditText will be sent to

Solution 1:

The quick and easy answer is to call getView().findViewById() from the activity (as shown below). The more proper approach is to give your fragments an interface that gets and retrieves the text. Below is the code for the quick and easy method, but when you create a fragment in Android studio it shows you how to do the more proper interface approach.

package org.pctechtips.myfragmentapp;

publicclassMainActivityextendsFragmentActivity {

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /*fragments 1 section*/FragmentManagerfm1= getFragmentManager();
    FragmentTransactionft1= fm1.beginTransaction();
    ButtonFragmentbf=newButtonFragment();
    ft1.add(R.id.button_fragment1, bf);
    ft1.commit();

    /*Fragment 2 section*/FragmentManagerfm2= getFragmentManager();
    FragmentTransactionft2= fm2.beginTransaction();
    TextviewFragmenttf=newTextviewFragment();
    ft2.add(R.id.textView_fragment2, tf);
    ft2.commit();

    EditTextet= (EditText) ft1.getView().findViewById(YOUR_EDITTEXT_ID);
    Buttonbutton= (Button) ft1.getView().findViewById(YOUR_BUTTON_ID);
    TextViewtv= (TextView) ft2.getView().findViewById(YOUR_TEXTVIEW_ID);

    button.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            tv.setText(et.getText();)
        }
    });
}

Solution 2:

You can do it by several ways. For example via Activity like in Official Docs. But, I'm prefer send broadcast messages via LocalBroadcastManager like in this answer of war_Hero:

In Sending Fragment(Fragment B - Your ButtonFragment)

publicclassFragmentB {

    privatevoidsendMessage() {
      Intent intent = new Intent("custom-event-name");
      intent.putExtra("message", "your message");
      LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
 }

And in the Message to be Received Fragment(FRAGMENT A - Your TextviewFragment)

publicclassFragmentA {
    @OverridepublicvoidonCreate(Bundle savedInstanceState) {

      ...

      // Register receiverLocalBroadcastManager.getInstance(this).registerReceiver(receiver,
          newIntentFilter("custom-event-name"));
    }

//    This will be called whenever an Intent with an action named "custom-event-name" is broadcasted.privateBroadcastReceiver receiver = newBroadcastReceiver() {
      @OverridepublicvoidonReceive(Context context, Intent intent) {
        String message = intent.getStringExtra("message");
      }
    };
}

And don't forget to unregister receiver:

protected void onPause() {
  super.onPause();
  LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
}

Post a Comment for "How Can I Update A Textview From One Fragment To Another"