If it needs to be accessed from different Activiti...
# android
p
If it needs to be accessed from different Activities/Services/BroadcastRecievers I would move it to the Application object, then inject it all the way down. I like it better than making it an
object
.
s
This is a library, so I don’t own the
Application
object. Otherwise, that’s what I’d do.
I’d really like to keep it as an
interface
. so that a mock one for testing can be created. The current approach that I’m taking is that in the constructor of the object, I’m assigning it a
Long
id
from an
AtomicLong
, and inserting it into an internal
object
where it’s held with a
WeakReference
. Then I pass the id of it into the
Activity
, and the
Activity
pulls it from the internal object that keeps track of any active instance. I’m not very happy with this approach though. It’s VERY hacky. I’m unsure of any alternatives though
s
@louiscad how does this solve my problem?
l
@spierce7 Now I read in your previous message out of the thread that you have a more complex requirement than I thought when I read
BroadcastReceiver
and
object
. It can help have the application Context for cases where the activity's one is not needed, but I think that's irrelevant for your need, I'm sorry for the noise.
s
thx for trying.
p
I see your point now. Initially I did not get well your intention. In such a case, I don't think what you are doing is too crazy. My only suggestion is changing the name of
CompanyAuth()
to
CompanyAuthClient()
or
CompanyAuthAccessor()
or
CompanyAuthConnection()
. Some name that indicates a user of this class that you don't talk directly to the
CompanyAuth()
implementation but to an intermediary class.
s
I thought of a happy medium yesterday. I maintain the interface
CompanyAuth
, with the function
CompanyAuth
to get an instance of it, but then I have the instance returned from the function to be an object:
Copy code
internal object InternalCompanyAuth : CompanyAuth {
  // ...
}
Copy code
fun CompanyAuth(): CompanyAuth = InternalCompanyAuth
p
I see, although I would insist in naming your "builder" function more descriptive. Something like
getCompanyAuth()
. Using
CompanyAuth()
sounds like you are creating a new class instance, and is not the case
s
that’s an interesting point