0

I have a bluetooth reading routine, and it gives me a String that I would like to "print" into a fragment's textview. The string is updated in real time, so it shall not be an onetime event.

Funny is that I can pass a TextView text from the Activity to the fragment's TextView. Then I put the String into the Activity TextView, and then from the Activity TextView to the Fragment Textview.

The Activity Textview is 0dpx0dp so nobody will see it.

How could I pass the String directly from the MainActivity to the Fragment's TextView?

This is in the activity:

        textAmanheceMenos.setText(dadosTratadosB);

This is in the fragment:

        TextAmanheceMenos = (TextView) getActivity().findViewById(R.id.textAmanheceMenos);

        String MSG = TextAmanheceMenos.getText().toString();
        TextAmanheceEm.setText(MSG);

Edit: This is working. But I had to create that ghost textview in the activity. The "textAmanheceMenos" textview in the activity must be eliminated. The String "dadosTratadosB" must go directly into the "TextAmanheceEm.setText(dadosTratadosB)". But that doesn't work. It says "cannot resolve symbol"

Even if I create:

 DadosTratadosB = getActivity().findViewById(dadosTratadosB); 

How could I make it work guys?

Thank you!

3 Answers3

0

To send data from your activity to fragment use this:

Bundle bundle = new Bundle();
bundle.putString("KEY", "STRING to send");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

then in your fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String recivedString = getArguments().getString("KEY"); 
    //Do whatever you want with this string   
    return inflater.inflate(R.layout.fragment, container, false);
}
sumit
  • 1,047
  • 1
  • 10
  • 15
  • Thanks! What are those Fragmentclass Arguments? The fragment already initiates somewhere else. What is that "KEY". Any String Variable? – Raphael Amin Jun 21 '17 at 08:35
  • KEY will be your variable you can pass it anything you want just remember you have to use same key when you'll retrieve that data in your fragment – sumit Jun 21 '17 at 09:25
0

I assume your fragment is attached to your activity and you need to send text to your fragment multiple times without recreating/reattach fragment. You can use local broadcast to receive text messages in your fragment: https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

Alternatively, look at EventBus library: https://github.com/greenrobot/EventBus it's easier to use and more flexible. You can send message from any part of your app and add subscibers to receive them somewhere else.

[edit] If you want set text in fragment only once, you can pass your text as argument, as it's described in above answer, then your fragment will contain text you have passed there

user1209216
  • 7,404
  • 12
  • 60
  • 123
  • I want exactly what you said. Send text to fragment multiple times without recreate or reattach it. That is an addon library? It's not on Android Studio right? – Raphael Amin Jun 21 '17 at 08:38
0

Create an Intent to pass data. See here

Try below code for passing value from Activity where real data is coming

private void forwardActivityResult(Fragment f, int requestCode, int resultCode, Intent data) {
    if (f != null) {
            f.onActivityResult(requestCode, resultCode, data);              
        }
    }
}

Inside fragment you can get value from Intent data and set it on TextView using data.getStringExtra

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "onActivityResult");
        super.onActivityResult(requestCode, resultCode, data);

    }
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53