0

I am creating a UDP messaging system for Android application. For that I have created a seperate class with sending, receiving, parsing threads. The class has to be instanciated only one - ie. only one UDP server!

Now, in Android you have multiple activities in your design which are basically different classes. I would like to use my UDP communication iterface from those different classes. But currently UDP CI is instanciated once in the main class. Therefor I cannot access the object from different classes. Moreover, the CI parse module will have to change settings/UI/... of the whole app depending on the message.

Therefore:

  1. Do I need to have instantiation at all if I do not need more than 1 object?
  2. Is it a good choice to have everything in the class static?
  3. At some point I will need to talk the other way round (obj->main). Should I use callbacks or just pass necessary resources from main to the CI and do things with them in CI?

I hope I described the problem clear enough. Please ask me if any questions arise.

3 Answers 3

2

Either store it in Application Context (which is a singleton). Or do the connection in Service and let each Activity retrieve data if they want.

Sign up to request clarification or add additional context in comments.

Comments

0

It seems like that your different classes need an instance of Or formally speaking have a dependency on UDP Cl. To address this, we have two options -

  • use a dependency injection framework e.g. guice to inject such dependencies automatically in different classes
  • Old school way - Create a singleton for UDP Cl and all the classes which need UDP Cl can do something like UDBCl.getInstance() to get instance of UDP Cl

I personally prefer method 1 because that is unit test friendly. Method 2 will be quicker to solve your problem

2 Comments

Could you please what is dependency a injective framework and how does it work? Any short example?
Please take a look at code.google.com/p/google-guice/wiki/Motivation (its gives a great background on how dependency injection helps and how to use it)
0

Create an instance of the first class in the second class.. Also, you can use bundles to transfer data between classes.

1 Comment

Data must be serializable for bundles. And I need to minimize object instantiation to minimize memory usage.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.