1

I am building an Android library for our platform and I'm struggling with how to allow my users to pass a function that gets called at certain times in the library.

The library starts an Activity, that activity has some service classes that point to my https API. (oversimplification but hopefully thats enough detail)

What I want to provide is a way for my users to pass a callback that generates the auth token for the API, allowing them to generate it themselves. Maybe the interface might look like String getAuthToken().

But how can I do this without losing state at some point? I started with a Singleton when the library starts up (it starts an Intent for the main activity of the library), but when the process gets killed and you try come back to it then of course the app crashes, the singleton was set up before the activity started.

Am I missing some obvious pattern in Java or Android that solves this?

Pure Function
  • 2,129
  • 1
  • 22
  • 31
  • 1
    What you mean with `SDK`? It stands for software development kit, I don't think you're creating that. – devgianlu Apr 15 '19 at 12:06
  • Sorry its an Android library. we are building an SDK for our platform. but its a library – Pure Function Apr 15 '19 at 12:14
  • there is no defined line between SDK and library. At which point the library became SDK? For example, you have a 10x15 photo and you slice it in half, could those slices still be name as photo? I'll say yes. But if you slice them 100000 times, could they still be named as photos? The same goes with a discussion about library/SDK. – Taier Apr 15 '19 at 12:21
  • Hopefully I have absolved all mentions of SDK now. and it is clear that I mean a library :) – Pure Function Apr 15 '19 at 12:24
  • 1
    I actually tried to defend you) but you are right, sometimes I come here to argue about stuff :) – Taier Apr 15 '19 at 12:28

1 Answers1

0

Why do you need an active context in a singleton? That looks like a bad design to begin with. You dont need it in order to make a networking requests.

When you present an activity, you take control of all lifecycles that it involves. Yes android re-create it when you rotate the screen and that a huge pain in the ass, but for that, you could use ViewModels:

enter image description here

Taier
  • 2,109
  • 12
  • 22
  • Hey I never said at any point that my Singleton needs context. I feel like I'm going crazy because I'm obviously missing something here that everyone else gets. I need my users to pass a callback. That callback is an instance. If the entry point to my library is an activity I can't hand it on to the activity right? It would have to be parcelable or serializable? How does a ViewModel help me for this case? – Pure Function Apr 15 '19 at 12:19
  • In that case just create a regular singletone, that could hold an array or objects, that implements your callback. When the time came, just interest over them and send a notification to the ones that still alive. – Taier Apr 15 '19 at 12:26
  • I have a regular singleton, but short of making it serializable how can I keep it alive when the process dies? – Pure Function Apr 15 '19 at 12:32
  • you can't, thanks to Java garbage collector. In a theory, if you singetlone is static, it would live as long as application. If it gets destroyed - if you be re-created by the same flow as it was created in a first place, since all application would be brand new. https://stackoverflow.com/questions/13891186/static-singleton-lifetime-in-android – Taier Apr 15 '19 at 12:36
  • ok thank you that is hugely helpful. Maybe it is a different issue I am having with my static singleton then. – Pure Function Apr 15 '19 at 12:38
  • come back if your problem persists, will try to come up with something :) – Taier Apr 15 '19 at 12:48