I'm trying to create an android project that works like that: it has an activity which has two fragments:
- connects to a remote device
- use the API of that remote device after it's connected.
so the fragments are:
- ConnectFragment
- Usefragment
in the connect fragment I have a method to connect to the remote device with a click on a button:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String ip = ((ConnectableDevice) parent.getItemAtPosition(position)).getIpAddress();
remoteDeviceController.connect(ip);
if (remoteDeviceController.isConnected()) {
listView.setVisibility(View.INVISIBLE);
} else {
Toast.makeText(getActivity().getApplicationContext(), "Coludn't connect", Toast.LENGTH_LONG);
}
}
});
once it's connected, I want to move to the other fragment: UseFragment
(after the line of listView.setVisibility(View.INVISIBLE);
) and I want to pass to that fragment, the object remoteDeviceController
since it has all the data of the connected device, and it is the one that already connected.
How can I pass the remoteDeviceController
object? should I create it in the activity so it will maintain it's state when after I switch between fragments? if so, how can I do it?