Despite similar question was asked, I have differnet situation:
My app consists mostly of a background Service. I want to start external activities and get results back.
I see several options:
-
Create dummy
Activityand keep reference to it for using itsstartActivityForResult. This consumes quite a lot of memory, as we know. -
Use
Broadcast Intentsinstead of Android’s results infrastructure: ask client activities to broadcast their results before closing. This kind of breaks the idea and not so performance-efficient. -
Use
Instrumentationdirectly – try to copy code from startActivityForResult into my Service. -
Use Service interfaces – serialize and add
AIDLconnection to the Intent for starting an Activity. In this case Activity should call Service directly instead of providing result.
The third approach feels closer to Android for me, but I’m not sure if it’s possible to do – Service does not have its Instrumentation, and default implementation seems to always return null.
Maybe you have any other ideas?
I think option 2 is the most idiomatic way on android. Using
startActivityForResultfrom anActivityis a synchronous/blocking call, i.e., the parent activity waits and does not do anything until the child is done. When working from aServiceand interacting with activities your primarily doing asynchronous/non-blocking calls, i.e., the service calls out for some work to be done and then waits for a signal to tell it that it can continue.If you are using the android local service pattern then you can have your activities acquire a reference of the
Serviceand then call a specific function after it has performed its work. Attempting your option 3 would be counter to what the framework provides for you.