0

i have a app that connect to bluetooth and get the data from the bluetooth device in an activity and i want to send the data i get to the fragment and i use Navigation drawer to get the fragment when i start the fragment it send the data when the fragment is created and does't send after that how to make the fragment receive the data after it change in the activity

her is where i start the fragment and send data to the fragment

    @Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    switch (id) {

        case R.id.nav_car:
            backgroundImageView.setVisibility(View.INVISIBLE);
            //setup the car fragment
             carFragment = new CarFragment();
            fragmentTransaction.replace(R.id.fragment_container, carFragment);
            fragmentTransaction.commit();
            carFragment.sendData(recDataString.toString());
            break;
    }

here is the interface

  public interface sendToDataToFragment {
    void sendData(String data);
}

this the fragment code

public class CarFragment extends Fragment implements MainActivity.sendToDataToFragment {


private static final String LOG_TAG = CarFragment.class.getSimpleName();

public CarFragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    //address= getArguments().getString(EXTRA_DEVICE_ADDRESS);
    View rootView = inflater.inflate(R.layout.fragment_car, container, false);

    // Inflate the layout for this fragment
    return rootView;
}

@Override
public void sendData(String data) {
    if (data != null)
    {
        Log.e(LOG_TAG,data);
    }
}

}

here is the code where i get the data from the bluetooth i want to send this data to the fragment this data is always change

   bluetoothHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            //if message is what we want
            if (msg.what == handlerState) {
                // msg.arg1 = bytes from connect thread
                String readMessage = (String) msg.obj;

                //keep appending to string until ~ char
                recDataString.append(readMessage);



            }
        }
    };

enter code here
Ahmed Abdelmeged
  • 2,299
  • 2
  • 18
  • 29
  • Check this [answer](http://stackoverflow.com/a/25392549/5993410) – Atef Hares Feb 23 '17 at 21:32
  • Possible duplicate of [Communicating between a fragment and an activity - best practices.](http://stackoverflow.com/questions/14247954/communicating-between-a-fragment-and-an-activity-best-practices) – Atef Hares Feb 23 '17 at 21:32
  • I suppose ur activity is the container of ur fragments. If that is true just create public/protected (if u are in the same package) methods on ur fragment with the data u want to update and then call them in ur activity, it´s very easy – Catluc Feb 23 '17 at 21:33
  • @Catluc the the information is updated in the main activity with handler but not displayed i want to send them to the fragment and send them i will edit the question check it – Ahmed Abdelmeged Feb 23 '17 at 21:53

1 Answers1

0

Just call sendData(String data) on a current fragment. To get reference to fragment currently displayed in your fragment container call:

sendToDataToFragment fragment = (sendToDataToFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentContainer);
fragment.sendData(data);
matip
  • 794
  • 3
  • 7
  • 27
  • ` bluetoothHandler = new Handler() { public void handleMessage(android.os.Message msg) { //if message is what we want if (msg.what == handlerState) { // msg.arg1 = bytes from connect thread String readMessage = (String) msg.obj; //keep appending to string until ~ char recDataString.append(readMessage); } } };` – Ahmed Abdelmeged Feb 23 '17 at 21:48
  • it case a null pointer because it did't start with the car fragment – Ahmed Abdelmeged Feb 23 '17 at 22:17
  • actually i solve my problem i get the bluetooth handler as static and i get implement it in the fragment and it's work for me thank you for your help – Ahmed Abdelmeged Feb 23 '17 at 22:19