async/await calls inside an Activity versus the various Background Services - Example Scenarios?

ChrisSwainChrisSwain USMember, University ✭✭

I'm trying to get my head around when it is appropriate to make an async/await call from within an Activity versus when I should create one of the various types of Background Services you can create in Android. For example, if I have a list that needs data from a REST service in the cloud, should I just make the call to it inside an async method in the Activity? Or should I get complicated and create a Bound Service that exposes a method to get the data and an event to notify the Activity that the data has been retrieved? It would be nice to have a few solid example scenarios defined for each of the various options.

Best Answer


  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    In my opinion it heavily depends on what are you trying to do. For example if you just want to present a list of data it would be suitable to just query them at the time you need them. Therefor an async method is good enough.

    If you need them regularly even if the app is in background or closed, a service would be a better implementation because it can run independently from an Activity.

  • ChrisSwainChrisSwain USMember, University ✭✭

    Thanks @SebastianS. That's a good start at answering the question. I'd love to see more example scenarios described where it makes sense to use just asyn/await or a Bound Service or a Started Service or a Hybrid Service. I think having a few examples described for each will serve as good guidance for when to use each approach.

Sign In or Register to comment.