Igor Igor - 7 months ago 70
Android Question

Passing reference to activity

I'm working on a simple multiplayer game on android. I have NetworkManager class which opens connection to the server and provides methods like send and receive.

public class NetworkManager {

private Socket socket;

public NetworkManager() throws IOException
socket = new Socket(HOST, PORT);



public void send(String msg) throws IOException { ... }


In one activity I connect to the server by creating new instance of NetworkManager. Then, I have to use that instance in several other activities. Is it possible to pass reference to it when starting new activity?


No, and you should not do it anyway. Use a service to do the background work: https://developer.android.com/guide/components/services.html - or at least an AsyncTask, but for your use case a service should be a better option.

You can only pass primitive or Parcelable types from one Activity to another.