Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can define an interface in <code>Fragment</code> B and implement it on the MainActivity. Then on the callback method (onClickOnB in this case) set the text on the <code>TextView</code>. You should obtain a reference of the <code>TextView</code> in the <code>Activity</code>'s <code>onCreate()</code> after <code>setContentView()</code>. This works because <code>Fragment</code> A is static. Otherwise, you can create a public method inside <code>Fragment</code> A so you can set the text from inside the callback by getting a reference of <code>Fragment</code> A and calling such method.</p> <p><code>Fragment</code> B</p> <pre><code>public class FragmentB extends Fragment implements onClickListener{ ClickOnB listener; public void setOnFragmentBClickListener(ClickOnB listener){ this.listener = listener; } @Override public void onClick(View v){ //stringMessage is a `String` you will pass to `Fragment` A to update its `TextView` listener.onClickOnB(stringMessage); } interface ClickOnB{ public void onClickOnB(String message); } } </code></pre> <p><code>MainActivity</code></p> <pre><code>public class MainActivity extends Activity implements ClickOnB{ @Override protected onCreate(Bundle savedInstanceState){ //Get a reference of `Fragment` B somewhere in your code after you added it dynamically and set the listener. ((FragmentB)getFragmentManager().findFragmentByTag("FragmentB")).setOnFragmentBClickListener(this); } @Override public void onClickOnB(String message){ //Set the text to the `TextView` here (I am assuming you get a reference of the `TextView` in onCreate() after inflating your layout. mTextView.setText(message); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload