0

I was conducting today interview with one of the candidates. I asked pretty common question: how to add to Android application possibility of playing music constantly, even when activity is destroyed when phone orientations changed. Expected answer was: create Service responsible for playing music. Instead of that candidate suggested to create some singleton in application.

Is this also a correct answer? I think it should be working - Singleton class will be load to memory and will stay in memory forever; my colleague suggest this singleton will be destroy with destroyed activity, because there won't be any reference any longer.

pstrag
  • 617
  • 5
  • 16
  • 7
    No offense but with these uncertainties why would you conduct an interview. – Mike Mar 03 '16 at 16:10
  • 3
    Why you interviewing people if you don't know the basics? – Sleiman Jneidi Mar 03 '16 at 16:12
  • 1
    please have a look http://stackoverflow.com/questions/3567667/android-when-to-use-service-vs-singleton – Sree Reddy Menon Mar 03 '16 at 16:16
  • If a candidate is able to confuse you and your colleague with an answer to this "pretty common question", I guess he didn't really care for the job after that point. – f1sh Mar 03 '16 at 16:16
  • @Erik I've seen this question and answer: http://stackoverflow.com/questions/3567667/android-when-to-use-service-vs-singleton, so I assuem it doesn't work well – pstrag Mar 03 '16 at 16:16
  • 1
    ok guys, I see your point and I totally agree, but it's not my decision. I just want to grade this candidate fairly, so help will be appreciated – pstrag Mar 03 '16 at 16:16
  • 2
    Your colleague is correct; A singleton can't stay in memory forever. Heck, even Activities get destroyed for no reason while in background, especially if the phone is left idle. I'd stick to the "service" aswer only, and ask really specific questions about why the singleton would be deemed OK, and challenge with the fact that GC can (and will) wipe anything, even when you think it wont. – Shark Mar 03 '16 at 16:18
  • @pstrag it is not fair to grade someone who knows as much as you do, or probably more – Sleiman Jneidi Mar 03 '16 at 16:19
  • @Sleiman Jneidi yep, probably I should hand in my boss a letter of resignation in this case, but no, thanks – pstrag Mar 03 '16 at 16:35

2 Answers2

5

Is this also a correct answer?

IMHO, no.

Singleton class will be load to memory and will stay in memory forever

No. Processes get terminated when they are not in the foreground anymore, to free up system RAM for other apps. The determination of exactly when this occurs is based on a lot of variables... one of which is whether or not you have a running Service.

my colleague suggest this singleton will be destroy with destroyed activity, because there won't be any reference any longer.

No. A singleton in Java is implemented as a static field. So long as that field has a reference to the object, the object cannot be garbage-collected. By default, merely destroying an activity will not magically set that static field to null.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

The only way to create a background task that keeps the application's context from being destroyed is to run a Service in the foreground by displaying a persistent notification that informs the user that it is running. An example given is more or less exactly what you were asking the candidate to provide:

For example, a music player that plays music from a service should be set to run in the foreground, because the user is explicitly aware of its operation. The notification in the status bar might indicate the current song and allow the user to launch an activity to interact with the music player.

Additionally, the singleton exists in the global scope and lives for as long as global scope does - typically until Android kills your application because it's not running any foreground tasks, not when the Activity that uses it gets cleaned up.

A. Rager
  • 1,950
  • 2
  • 15
  • 16