0

So I have a Custom object called connector which contains a threaded socket for communicating with an Arduino without locking the UI. But I want to access this socket from multiple screens/activities.

I tried using a static class to contain the object like in this answer:

Transfer Socket from one Activity to another

But I keep getting a "name does not exist in current context" error when trying to acces it from antoher activity.

here is the code for my storage class:

public class ConnectorManager
{
    private static Connector connectorObject;

    public static Connector GetConnector()
    {
        return connectorObject;
    }

    public static void SetConnector(Connector connector)
    {
        connectorObject = connector;
    }
}

edit1: this is how im trying to use the class:

ConnectorManager.SetConnector(connector);
Connector connector = ConnectorManager.GetConnector();

edit2:

after deleting the code file and creating a new one the problem magically fixed itself

Jelle Bleeker
  • 131
  • 11

2 Answers2

0

could you please add the code sample, how did you call the static variable connectorObject in different activities?

0

I would go with the second approach mentioned in the example you linked too. In fact I am doing this for most of my communication stuff.

[Application]
public class AppApplication : Application
{
    public ConnectorManager ConnectorManager { get; set; }

    public AppApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
        //if you can/want create and assign your manager here
    }
}

In any activity you can access the application property like this:

((AppApplication)Application).ConnectorManager;
tequila slammer
  • 2,821
  • 1
  • 18
  • 25