0

I came up with this question because I realized that I'm currently doing both in my program and both methods work.

For transfer of the value of an integer timeToUse from my activity to my service, I am using putExtra to store the value in my intent and then getIntExtra in my service to pull the value from intent.

For the transfer of the value of a boolean notificationSounds from my activity to my service, I am calling a method that returns the context of my activity, then using the context to call a getter method that returns the value.

Are either of these methods better/safer than the other? Why?

7
  • If you can omit a Context as parameter pass, then you should. Jan 13, 2015 at 8:59
  • Any tips on how to do that with a service that is already running? Can I just replace the value if I have to change it by using putExtra again with the same key string? @stealthjong
    – JayB
    Jan 13, 2015 at 9:14
  • Why not do with the boolean exactly as you did with the integer? Jan 13, 2015 at 9:58
  • @stealthjong because the integer is a one-time selection variable for the user and becomes constant, the boolean needs to be updated when the user selects on/off for notification sounds in the gui which can happen at any time
    – JayB
    Jan 13, 2015 at 10:49
  • 1
    The more you pass the context around, the greater the chance that a reference to the context is kept. And when that happens, the GC can't clean the activity up, because it's still referenced. And that can cause memory leaks. So be careful when passing around contexts Jan 13, 2015 at 11:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.