I'm making an app in conjunction with a websites API, and in the application I created a class that would deal with the OAuth authentication and API calls. Now, how would I be able to instantiate an object from that class, and share the object across various Activities? I'm quite sure what would be the best practice in a situation like this.
Thanks a bunch!
You could create a subclass of Application and store your authorization module there. This would be accessible to all of your activities within that process.
You just need to declare you Application subclass in your manifest and it will be instantiated instead of the default.
From your Activity, you would call getApplication() and cast it to the correct subclass type and access your custom methods
Related
I'm trying to load external classes dynamically in an Android app, using a proxy object to intercept calls to the methods. For definition, for use a proxy object, classes must implement an interface. I thought if I create a proxy with an interface I will be able to call classes that not implement it directly. I tried use the same package name and interface implemented in the external class, but it doesn't work.
I have tried some solutions, but I'm not able to load classes dynamically because I receive a ClassCastException.
So I want to intercept object calls and do a function that is implemented in a external apk, using DexClassLoader to load it. I think it is not possible using proxy pattern.
Any ideas?
Coming from Java EE to an Android application (i'm not Guru in Java EE), For a fatest load of the application, we decided to process a files asynchronously, when the application starts, we launch some asynchronous channels to read the files, then we open the main activity, the idea is the user may not go directly to the use cases which need these files, but if he go it direclty and the asynchronous read wasn't terminated, he should wait a while(what the client wanted).
My question is how to passe the Future object between activities? my team agreed for using static methods or singletons, but i really don't love singleton because its an antipattern and i don't like static methods because they behave nearly like singleton.
Trying to use parceable or serializable, but future doesn't use implement anyone of it, i tried to use RoboGuice (its based on static fields but its busniss) but its a little buggy, how can i do it in android in pretty way, if there's another idea, i will be obliged to use singleton or static methods.
EventBus can be used to send objects from 1 place to another (e.g. asyncTask to Activity). It's great and easy to use, and it will accomplish what you're trying to do perfectly.
You can create your custom object classes to send between almost any 2 classes, Activities, Services, or whatever you with.
In a java project i need to call (lets say generic, utility) web services. For instance giving a city code as parameter and getting details about that city. Web services are already implemented and i can only consume them.
I had the same situation before in another project and created a class for that kind of webservices. That class had several web service call methods and all were static methods.
Now, i dont really want to do same thing again because i dont think thats the right way to do it (hard to debug etc). I also dont want to make a different class for all these methods and make an instance for each call because they are too generic and instantiation seems like an overhead for that situation.
So, alternatives comes to my mind is
Using old method. One static class, several methods.
Singleton class. Most probably will have syncronisation problems, so will have overhead using locking mechanisms.
Both are not the best solutions what would you suggest?
Thanks in advance.
I'm writing an Android app that has some functionality encapsulated in an internal library. However, for this functionality to work, the library needs an instance of the Application Context. What's the best way to give the library this Context? I see a few options, none of them appealing:
Have my library classes extend Application, and call getApplicationContext()
This is generally discouraged
Have my library classes each implement the singleton pattern, and have each caller pass in a Context each time they get a reference to the singleton.
This requires every caller to retrieve the Application Context before using the library, and also requires that the caller call against an instance of the library instead of against static methods defined on the library class (and thus further requires keeping a reference to this instance).
What's the best way to give the library this Context?
Pass a Context into the methods exposed by your library that need a Context. This is what the Android SDK does in places.
Or, change your library to expose objects, not static methods, and have the objects hold an instance of Context (supplied to the constructor or factory method that created the instance).
Have my library classes extend Application, and call getApplicationContext()
If you can call getApplicationContext() on a Context, you would just do that, without needing to subclass Application.
This is a solution that I found, which I have not tested, but is used by Firebase apparently to avoid creating an init method:
"What happens on Application start is, that it registers all ContentProviders in the system (calling onCreate). This means that at this point no activity has been started, but we have access to the (Application)Context, where we can initialise our library using this Context"
Essentially you are utilizing the onCreate of the empty ContentProvider as the init. It is hacky, but seamless.
https://medium.com/#andretietz/auto-initialize-your-android-library-2349daf06920
I am not sure best practice for this case but i would like to set the Context in my singleton class during Application onCreate.
MyLibrary.init(this);
Why?
If you had use Crashlytic/Fabric before, you will found that they are using
Fabric.with(this, new Crashlytics());
If you read the code in Fabric will found that they store the Context in Fabric singleton.
Facebook Android SDK did similar thing:
FacebookSdk.sdkInitialize(getApplicationContext());
If your browse their codes will found that they store static Context.
Well, which are the best practices? I am not sure but i believe those are awesome developers create awesome libraries and they did debate on this before come out this libraries. Anyway, I am still learning how to write best android library and come across this topics.
App Startup library is the part of Android Jetpack. It allows to listen application startup without the cost of multiple ContentProviders. Also, it provides the ability to handle dependencies tree.
Implementation example
Im writing an web application using the MVC design pattern.. the application should connect to a RMI server providing the business part (Model)
my problem is, I dont know where i should instantiate the model class and connect to the RMI, providing access to all servlets.. i added a ServletContextListener and shared the reference on the servlet context, but i dont think that is the right way to do it
thanks in advance
You can instantiate your Model/Business classes from your Controller. Better still make a single instance (Just Create One) of each in the start of the application. You can also go lazy-loading about it.
Instantiating Model/Business classes from your JSPs means, instantiating them from View. That, of course, violates the MVC pattern.
Either you can make your Model/Business classes as Singleton, or keep the reference in Servlet Context. Both are fine and the latter is more test friendly. The former can be made test friendly too, if you don't specify the constructor as private but just have a common understanding that it's not there for instantiating it everywhere.