It is necessary to transfer a complex object to another Acitvity in Android. Why does not it go out to use Serialization or Parcelable and pass through Intent: I want to transfer the TcpServer class in it I have two nested classes-threads (Thread).

That is, when creating this class, TcpServer starts the first stream (it starts ServerSocket and waits for clients). With accept (client connection), this thread creates a second thread for a specific socket. So here in the TcpServer class, I made a write () method that sends a message to all current clients. And just so seralizovat this object will not work. And not only that - I need the very same object (and not a new one) on the second activation. How can I do that? Purpose: I just need to send messages to all clients from several Activities.

  • 3
    What prevents it just to make it into the background service, as prescribed by the ideology of Android? An activity should not do anything long and heavy at all - it is responsible for the user interface. - Sergey Rufanov
  • I fully support the service, but I recommend EventBus github.com/greenrobot/EventBus/blob/v2/HOWTO.md - ivansoft for sharing between parts of the project

1 answer 1

There is a wrong but simple way: Use a static field. And through this field to transfer \ get access to the object.

There is a correct, but more complicated way. This is the way described by @SergeyRufanov:
Create a service that will initialize and provide access to the desired object, and you will be able to access the service from any Activity using a bind.

There is one more way that will correctly be in the middle between the above:
Store the desired object in the Activity, and transfer it to fragments. That is, translate your current Activity into fragments, and create one new Activity that will store the object.

Once again, the correct is the second method, with the creation of the service. I highly recommend it.

  • 2
    You will not mind if I edit the answer to the points I divide? .. =) - Yuriyi SPb