Search code examples
androidandroid-activitybackground-process

Android activity in background?


I'm developing an application that starts from an activity A (main) and passes through login to an activity B, then C, and so on...

Now, the need arose, at login, to start a certain algorithm that spins in a loop for the whole duration of the application. Is it possible to create an activity that operates (in the background) in parallel to the stack of visible activities? Or are there other modus operandi that you know?


Solution

  • What you are looking for is a Service. As per the documentation:

    A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

    See the provided link for examples and more information.