Issue
In my app, I put all the global used variables and methods in a singleton Application
class. It works fine, but the code looks messy and difficult to reuse in other project, because variables and methods are for different use, such as network operations, memory management or some other project-related stuff.
Is it OK to split the variables and methods into different classes, and call static methods instead, while acting the same as they're all in a Application
singleton class?
Solution
Yes. Feel free to use statics etc as you normally would in Java code, taking the Android lifecycle into account (including when the process dies and your static values disappear).
That said, any kind of global state has issues, mostly around surprising complexity and hidden behavior. There are many ways to mitigate that and they aren't unique to Android.
One solution that can make things more manageable is using dependency injection and a @Singleton
designation - dependency injection has its own set of tradeoffs but I think it's a net win. Check out Dagger, the best publicly-available DI framework I know of that's optimized for Android.
Answered By - orip
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.