Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – How to pass a service-object to an activity in 2020

Posted on November 14, 2022

Solution 1 :

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:

  1. implement your service following the sample LocalService
  2. config in AndroidManifest.xml:
    <service android_name=".YourNewService" android_enabled="true"/>
  3. for activities needs to interact with the service, just implement them similar to BindingActivity of the guide. (No worries on service instantiation, singleton, etc.)
  4. 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!

Problem :

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 ?

Comments

Comment posted by mcfly soft

Please comment when negate. Whats wrong with this question ?

Comment posted by March3April4

I don’t know the PROPER way to do so, but will using eventbus or something like eventbus using livedata do the job?

Comment posted by mcfly soft

Thank you. You guided me to the right way. I always used Singletons for this, but this is better.

READ  [ANSWERED] android - I there any way to have a scrollable composable inside a nestedscrollview?
Powered by Inline Related Posts

Comment posted by Tech4Happie

I guess you started with AIDL. In case your app/service requires IPC and really needs to use AIDL, you can use singleton at the application level where an instance of service is held and handled there: start, bind, broadcast to registered activities to inform that service was bound, and stop, etc. For activities that need interaction with the service, they need to register a callback when the service is bounded and get bounded service from the singleton application. Be thread-safe in code then.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress