0

I'm trying to create an android project that works like that: it has an activity which has two fragments:

  1. connects to a remote device
  2. use the API of that remote device after it's connected.

so the fragments are:

  1. ConnectFragment
  2. 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?

Finer
  • 19
  • 1
  • 8
  • Does this answer your question? [How to pass data between fragments](https://stackoverflow.com/questions/5194548/how-to-pass-data-between-fragments) – chand mohd Sep 06 '20 at 10:06
  • I don't think so because it is not a UI component, it's a some kind of a controller object, which I want to keep it's state if the user switches between other fragments. – Finer Sep 06 '20 at 10:20
  • @Finer could you explain I want to keep it's state? – Anchit Sep 06 '20 at 10:50
  • @Anchit the `remoteDeviceController ` object has sockets that it opens and receives messages and data from the remote device and it saves it in a queue that I don't want it to get lost. – Finer Sep 06 '20 at 11:00
  • @Finer make the queue publically static or try moving the data back and forth using Bundles – Anchit Sep 06 '20 at 11:04
  • @Finer please follow Ankit's suggestion or you can do it using Service class and that class will interact with all of your fragments and activities. – Basu Sep 06 '20 at 11:33

0 Answers0