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