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 – Best way to share same data instance between an Activity, Fragment and Service?

Posted on November 14, 2022

Solution 1 :

You can create a ViewModel with activity scope, that will be shared between the Fragment/Activity/Service.

class AudioPlayerViewModel: ViewModel {
    val songLiveData = MutableLiveData<Song>()
    
    fun playSong() {
        // Todo your logic
    }
    
    fun stopSong() {
        // Todo your logic
    }
    
    .
    .
    .
}

Every change you will do to your song will be done through the ViewModel and the Fragment/Activity/Service will observe to changes happening in the songLiveData and update the UI accordingly.

As for network requests I suggest you take a look at the retrofit library. A common practice is to use the Repository pattern that will hold all the network and DB calls after that you can get an instance of the repository inside your ViewModel and execute that Network call. You can either use coroutines or RxJava to handle the threading.

To summarize:

Activity/Fragment/View/Services…

Responsible for UI interaction the observe data from ViewModels and do UI changes accordingly to that Data.

ViewModel

Have the responsibility of an interactor between the network layer and what the users see. They modify the data so the UI can stay dumb and not hold any logic.

Repository

Responsible for API & DB calls.

Problem :

I am an amateur android developer and I making a Music Player app and I need have to have the same object instance (Song object) in my Activity, Fragment and Service and all of them are capable of manipulating the state of object. User can play/pause/next/prev a song and all of them needs to show the same current song information and state of the song if it is PAUSED or PLAYING.

My Activity can change the song state through the bottom layout that is persistent on all fragments.
My Fragments can change the song state through choosing/pausing songs through the playlist page.
My Service can change the song state through the notification bar by pausing/resuming/next/prev a song.

READ  [ANSWERED] android - Starting WorkManager task from AppWidgetProvider results in endless onUpdate calls
Powered by Inline Related Posts

I have trouble thinking of a solution on where and how to store the object instance.
I thought of having them all have a Song object instance and just update the value through sending broadcasts but I don’t think this is the best solution and looks pretty sloppy.

The song URLs to be played are also retrieved from a server. Where is the best place to make the API calls?

It would be greatly appreciated if anyone can suggest a better approach on how to do this. Thank you!

Regards,

Comments

Comment posted by M D

Make singleton object and save everything over there

Comment posted by github.com/greenrobot/EventBus

use event bus library

Comment posted by gmatcat

Thanks for the reply Victor! I’m leaning towards implementing your suggestion but I read somewhere that accessing a viewmodel inside a service is not recommended as optimally a viewmodel should only be connected with a UI View. And the only accepted constructor parameters for “this” on ViewModelProviders.of(this, factory) are only Fragment or FragmentActivity.

Comment posted by insert-koin.io

I see, well it really depends on what you need to do in the service… You can take advantage of the Singleton pattern or use Dependency Injection to reference the same class that will be changed from all places. You can take a look at

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