I have:
a MainActivity
a OtherActivity
a DatabaseService, which is instantiated in MainActivity and which holds Sqliteconnection and functions like List<data> getData()
How do I pass the DatabaseService to my OtherActivity ?
So far my DatabaseService was a Singleton and I referenced that Singleton from the OtherActivity, but I dont like that Solution.
I can't also pass it with putExtra, because the Object is too complex to serialize.
There should be a more easier way to inject or pass the Service to the activity.
Does anyone have any hint for me ?
What you need to do is binding your activities to the service (in your case: DatabaseService). Highly recommend you follow [this dev guide] https://developer.android.com/guide/components/bound-services and choose the simplest way to create a bound service: extending the binder class. So for short, what you need to do:
implement your service following the sample LocalService
config in AndroidManifest.xml:
<service android:name=".YourNewService" android:enabled="true"/>
for activities needs to interact with the service, just implement them similar to BindingActivity of the guide. (No worries on service instantiation, singleton, etc.)
Please note that you always can create a base BindingActivity class that handles all binding cores and let other activities extend the base binding class.
Happy coding!
Related
I want to bind my custom firebase service to the MainActivity class. if it was a normal service, it would be easily done by making the service implement the onBind method, but it is not available because this method is set final in the superclass of FirebaseMessagingService class. I want to bind the service in order to use the LiveData variable set inside this class in the MainActivity class.
I have searched for best practices here and read this documentation about bound services but no luck. I tend to use LiveData somehow but I feel it is not working until I somehow manage to bind the service. am I missing something?
As suggested in a comment by #CôngHải, I can use a singleton object with LiveData inside it to communicate between service and activity without the need of binding the service to activity.
I'm creating an Intent for a Service that I have:
_myServiceIntent = new Intent(this, typeof(MyServiceCls));
This works, but MyServiceCls has dependencies. Is there any way for me to pass these in to MyServiceCls rather than create instances of them in the default constructor?
The simplest way of sending data to a service is through intents, but that will mean the parameters are serialized/parceled, so not helping ...
I see two ways of doing it:
The complicated solution: You could create a binded service with remote messenger; once the service is connected, you can send to the Incominghandler a Message for each reference you need. But thinking again, I believe that will serialize the object and the service might get another instance ... So, maybe the simple solution is the best for you:
The simple solution: Why don't you use an IoC (Inversion of Control) mechanism? You could integrate RoboGuice, AndroidAnnotations or Dagger.
2.1 If you don't want (it's not the time right now) to integrate above APIs, you could have a singleton class in the project that keeps the instances you need, you would initialize this class in Application.onCreate(), lazy load (or not) the objects you need and provide getters for each. And then in your service you call call get for each of those objects you need from singleton class.
So I'm trying to make an application using the Singleton Method. I want to have a class that stores all the information about my device's bluetooth state/connections/devices, and I want to make multiple activites that can access these methods.
I know that I need to have a class that extends Application, then I can access everything by calling getApplication(). What I do not understand, is where I initialize this object. From my frame of reference, I have all of these separate Activities, and if I initialize the object in one, I'm going to need to use intents to pass the object to the next activity, which completely defeats the purpose of using the singleton method.
Any help would be appreciated, thanks.
Simply extend from android.app.Application. Then register it as the application class in your AndroidManifest.xml:
<application android:name="mypackage.MyApplication" ...>
In your class you will receive usual Android calls, such as in
#Override
public void onCreate() { }
where you will able to initialize your global instances.
In the activities fetch the instance of MyApplication downcasting with:
MyApplication app = (MyApplication) getApplication();
Hope that helps.
If you extended Application your class will be created as your app launches. It can be retrieved in Activity classes using getApplication()
Check here : http://www.kodejava.org/examples/12.html
and here : http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/
and here : http://inchoo.net/mobile-development/android-development/android-global-variables/
I want to implement my database helper class as a service, but it already extends SQLiteOpenHelper... is there an interface for Service available in android?
Dunno if it is a good idea, but anyways, using inheritance does not make a lot of sense in this case - you should go with composition. Your service should extend Service class and include DatabaseHelper as an instance variable.
I have my own singleton class which inherits from Application. In all my activities I get this shared class by using DataClass dc = (DataClass) context.getApplicationContext();.
this seems to work but when I trie to acces the singleton in a custom BroadCastReceiver I have an other instance of the Application.
Is that possible? Does anyone have a solution for this?
This is expected behaviour.
If i am not mistaken, a BroadcastReceiver runs on a different process then your activity. That means you'll have a different instance of Application on your BroadcastReceiver.