Application Lifecycle Management
Mobile applications development has to deal with the concept of application lifecycle. With this we mean that mobile apps are created to manage scenarios where batterly life, CPU and memory are limited (as opposed to the classic desktop app where all of this is unlimited).
When a mobile app is running in backgroud it is suspended by the OS after a few seconds. The exact timing is different from OS to OS and other factors. When the application is suspended it is frozen: the app will continue to use memory but all the running operations are stopped. This way, every other application can make use of resources. However RAM isn't infinite and the app will be killed by the OS to free some memory if necessary.
As mobile developers you need to take care of this to provide your users a smooth and transparent experience whenever possible and restore the previous state of the app.
The typical application lifecycle events are:
- Starting. This happens the first time the app is launched.
- Resuming. This happens every time we restore the app from the background after it has been suspended.
- Sleeping. This happens when the OS decides to freeze our app after it has gone in background.
The management of these events can be tricky in an MVVM app but Prism provides the IApplicationLifecycleAware interface to make your life easier.
How to handle ALM in your ViewModels
The first thing you have to do to is to implement the IApplicationLifecycleAware
interface in your ViewModel class. Implemening this interface means that you have to provide an implementation of the OnResume()
and OnSleep()
method that are called by the framework when the app is resuming and going to sleep respectively. The typical operation you'll do in the OnSleep()
method is to save the state of your ViewModel to later restore it during the execution of the OnResume()
method.
The following is an example of a ViewModel that implements IApplicationLifecycleAware
.
public class ViewModelExample : ViewModelBase, IApplicationLifecycleAware
{
protected readonly IApiClient apiClient { get; set; }
public ViewModelExample(IApiClient apiClient)
{
this.apiClient = apiClient;
}
public void OnResume()
{
//Restore the state of your ViewModel.
}
public void OnSleep()
{
//Save the state of your ViewModel.
}
}
How to handle ALM at Application level
You can handle ALM events at the Application level by overriding the InvokeOnSleep()
and InvokeOnResume()
methods in the App
class.
The following is an example of an App
class with ALM management.
public partial class App : DEMApplication
{
protected override void InvokeOnResume()
{
base.InvokeOnResume();
// TODO: Refresh network data, perform UI updates, and reacquire resources like cameras, I/O devices, etc.
}
protected override void InvokeOnSleep()
{
base.InvokeOnSleep();
// TODO: This is the time to save app data in case the process is terminated.
// This is the perfect timing to release exclusive resources (camera, I/O devices, etc...)
}
}
Handling app resume and suspend
In general, an application goes into sleep mode when it no longer commands the screen and has become inactive. From this sleep mode, an application can be resumed (signaled by an OnResume()
call) or terminated. But this is important: after the OnSleep()
call, there is no further notification that an application is being terminated. The OnSleep()
call is as close as you get to a termination notification, and it always precedes a termination. For example, if your application is running and the user turns off the phone, the application gets an OnSleep()
call as the phone is shutting down. If your program has established a connection with a web service, or is in the process of establishing such a connection, you might want to use OnResume()
to restore that connection. Perhaps the connection has timed out in the interval that the program was inactive. Or perhaps some fresh data is available.
*Other resources