2

I'm writing some library code distributed as a jar file that developers will need to initialize with an application id before using. Initialization is just a function call, like

MyLibrary.initialize("16ea53b");

The tricky thing is that I am not sure how to instruct developers to make this initialization call. At first I thought a single static initializer block in the main activity would be the easiest way to do it. The problem is a user could enter the application through some other activity or intent, and the main activity would not be loaded. Is there a general way to ensure that a line of code is run at the application's startup regardless of how the application was started?

The initialize call is idempotent so I could just tell people to make this initialization call in every place it could be used, but that would be bothersome.

lacker
  • 5,470
  • 6
  • 36
  • 38

2 Answers2

0

This sounds like a problem that can be resolved by extending creating a class that extends Application and placing it there, which is global for the entire Application.

Ashterothi
  • 3,282
  • 1
  • 21
  • 35
  • For an individual app, this would make sense. For a library developer, I would rather not explain to every user how to subclass Application. – lacker Jul 18 '11 at 22:37
0

One easy way is to save something in SharedPrefences when your library code is initialized. And then, wherever you deem important, you can check for this value, and continue if it exists or prompt for initialization or anything (error messages etc). This will also allow your developers to not have to initialize more than once.

Be sure to provide the developers an API to reset this value.

Also, here is a good talk on API design that may help you, by Joshua Bloch.

omermuhammed
  • 7,365
  • 4
  • 27
  • 40
  • I think I have the same problem with how to give my library access to SharedPreferences - I will need to have the developer initialize the library with a valid Context in order to access the SharedPreferences. Or is there another way to access the Application object? That's a great talk by the way. – lacker Jul 18 '11 at 22:56
  • According to Android guidelines, we can subclass from Application class and thus use it to store/maintain global data or use static singleton classes. I personally have used Application object. So yes, your method of developer initializing the library with valid Context makes sense. I would love to know more about any other mechanisms for this. – omermuhammed Jul 18 '11 at 23:04
  • I still have the same problem - where does the developer initialize the library with a Context? They would have to do it from every class that might start the application. – lacker Jul 19 '11 at 00:24
  • Assuming you would want to go with this solution, then one way would be to make the developers write their Application class inheriting from your Library Application class (explained somewhat incoherently here: http://androidblogger.blogspot.com/2010/09/android-library-projects.html). Hope this helps, Good Luck! – omermuhammed Jul 19 '11 at 01:43