1

For a start, there are plenty of questions and answers about this subject, but my question is about what is the best practice in my specific case.

To keep the question short, my app will have:

  • Service to keep running after user minimize/end the app.
  • Service runs a task every 30min looking for new info.
  • Service shows notification when new info found.
  • When user start/restore the app, the new info should show on the UI.

I don't see the point of continuously updating the UI on a hidden activity, so what is the best solution here?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
masood elsad
  • 434
  • 2
  • 6
  • 18
  • Update the UI only when the app resume, the method to implement is `onResume`. – anemomylos Sep 12 '17 at 20:08
  • But the onResume will be an activity method, and all the new information will be in the Service. also the service runs a task looking for new info every 30min. – masood elsad Sep 12 '17 at 20:13
  • 1
    You have more than one option to do that. You can create a class with static methods that store/return the data. You can save the data in the database. You can save the data in a file accessible from both the activity and the service. – anemomylos Sep 12 '17 at 20:18
  • Thank you. I think I am going for a Singleton Class. Got the idea from here https://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities – masood elsad Sep 12 '17 at 20:51
  • Please answer your own question and accept the answer. That will remove the question from the list of unanswered questions and may help someone else. – David Wasser Sep 18 '17 at 16:54

1 Answers1

1

After considering lots of options, specially on this page

http://stackoverflow.com/questions/4878159/

I decided that I am going for a Singleton Class. I like how ease it is to work with, and serve the purpose for now

I highly recommend the link above, it has multiple options and you might find one that suits your needs better.

masood elsad
  • 434
  • 2
  • 6
  • 18
  • You will get bitten by using a Singleton to share data in your application. This is not an acceptable way to manage persistent data in an Android application. If your application is killed in the background, it will be restarted from the activity where you left off. If that activity was the activity accessing the singleton and not setting the data in the singleton you are very likely to run into null pointers. Please, everyone, do not take this advice or approach. Use an intent to pass values, shared preferences, use a SQLite database, a Realm database, or some other suited solution. – Dave Thomas Sep 19 '17 at 22:50
  • I didn't advise to use singleton, I advised to take a look at the link – masood elsad Sep 23 '17 at 00:25
  • No, you didn't but it is frustrating that the fellow that got that many upvote answer even suggested it. It's fine you want to learn the hard way, Singletons are not a good choice for that scenario. Sorry if I offended you in any way. – Dave Thomas Sep 23 '17 at 02:41
  • No worries. Thanks for coming back – masood elsad Sep 24 '17 at 15:07