0

I am currently working on a media player streamer project, and to facilitate keeping track of the current song, current progression, playlist, etc, I thought about creating a static class with those fields so I can update them from anywhere, including my Service, and also access the updated values from everywhere else.

My question is: is such approach a bad practice?

Just as extra information, I have a fragment that upon a track is selected, opens a DialogFragment with a media player. The user can close the DialogFragment, but the song will still play. Later on, the user can either select the same song or click on the "now playing" button, and the DialogFragment SHOULD resume where the song currently is. Right now it doesn't do that... Therefore, I thought about the above scenario.

If that is indeed bad practice, how would you guys normally handle this type of situation?

Thanks! =]

Felipe
  • 5,126
  • 5
  • 18
  • 21

1 Answers1

0

What you are dealing with is basically singleton problem. Use it or not to use it? Using static variables in Android. Loading MediaPlayer into memory when app starts is not a good practice in my opinion. Try to use Bind Service instead. If you are only storing simple values, you are free to go, but always be "thread safe".

Community
  • 1
  • 1
Michal
  • 1,008
  • 1
  • 12
  • 16
  • Hey @Michal, I have an instance of my MediaPlayer inside my Service, which I bind to my DialogFragment (with the player controlls). I think the problem might be that I am creating an instance of the dialog every time I open it, so a singleton might be a great solution. Let me give it a try! – Felipe Aug 23 '15 at 16:31