Reputation: 607
I have this bluetooth headset which sends messages everytime to the main Activity. Within the main activity, I have a handler which will process these messages and output accordingly.
I have a fragment which will make use of the messages. I cannot find a way to send live data from MainActivity to the fragment and I don't want to copy/paste the handler code to the fragment.
Is there a way to send live data from the mainActivity to the fragment. I am including some code for you to better understand.
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_dashboard);
//Bluetooth headset which send live data
tgDevice = new TGDevice(bluetoothAdapter, handler);
tgDevice.connect(false);
}
/**
* Handles messages from TGDevice
* @param menu
* @return
*/
private final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case TGDevice.MSG_STATE_CHANGE:
switch (msg.arg1) {
case TGDevice.STATE_IDLE:
break;
case TGDevice.STATE_CONNECTING:
//androidVersion.setText("Connecting...\n");
break;
case TGDevice.STATE_CONNECTED:
//androidVersion.setText("Connected" + "\t" +wifiInfo.getSSID());
tgDevice.start();
break;
case TGDevice.STATE_NOT_FOUND:
//tv.append("Can't find\n");
break;
case TGDevice.STATE_NOT_PAIRED:
//tv.append("not paired\n");
break;
case TGDevice.STATE_DISCONNECTED:
//tv.append("Disconnected\n");
}
break;
case TGDevice.MSG_POOR_SIGNAL:
poorSignalValue = msg.arg1;
actionBar.setTitle("Signal: " + poorSignalValue);
Log.v("Poor", "Signal: " + poorSignalValue + "\n");
break;
default:
break;
}
}
};
The Fragment will make use of the poorsignal as well as other variables. What is the best way to do this? Other fragments will also make use of the TGDevice.
Upvotes: 0
Views: 1367
Reputation: 612
You can use Android View Model and Live data to share achieve the desired behaviour.
//View model
public class SharedViewModel extends ViewModel {
private final MutableLiveData<Item> selected = new MutableLiveData<Item>();
public void select(Item item) {
selected.setValue(item);
}
public LiveData<Item> getSelected() {
return selected;
}
}
//Fragment
public class MasterFragment extends Fragment {
private SharedViewModel model;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
}
}
Make sure you pass ViewModelProviders.of(getActivity()) instead of ViewModelProviders.of(this) when calling from fragment.
For more information: https://developer.android.com/topic/libraries/architecture/viewmodel.html#sharing
Upvotes: 0
Reputation: 4462
look in this link: http://developer.android.com/training/basics/fragments/communicating.html
you can call a function of the fragment from your main activity.
public class Main extends FragmentActivity{
MyFragment articleFrag = (MyFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_id);
MyFragment.function();
}
public class MyFragment extends Fragment{
....
public void function(){
//do something ....
}
}
Upvotes: 0
Reputation: 5105
I'd use something like EventBus. Your Fragment
will register to an event, and your Activity
will post updates.
Upvotes: 1
Reputation: 3430
Implement observer pattern where the fragment acts as an observer and the activity subject.For more info refer:
http://en.wikipedia.org/wiki/Observer_pattern
You can also refer this link:
http://developer.android.com/training/basics/fragments/communicating.html.In this link the fragment acts subject and the activity as observer.You need to do the reverse.
Upvotes: 0