Original: Quick build Windows 8 style app 30-app lifecycle management
Introduction
In Windows 8, you can launch multiple apps and switch between them, and there's no need to worry about slowing down the system or draining the battery. Applications that are running in the background are automatically suspended (and sometimes terminated) by the system. Well-designed applications can be suspended, terminated, and restarted by the system, and these processes appear to be running in the application.
First, the principle
1. When the app is activated, the system sends Activated events for whatever reason
2. Whenever the user switches to the desktop or another app, the system hangs up your app and the system sends the suspending event
3. Every time the user cuts back to your app, the system will restore your app and the system will send the resuming event
Life cycle Changes
When the user launches the application, what changes can the app take in the life cycle?
1. Running applications that can be suspended to become suspended applications
2. Suspended applications that can be restored and become running applications
3. Pending applications, if the system is low on memory, will be killed directly
Changes in the state of multiple applications when switching to each other
1. Enable App1
2. When APP2 is enabled, APP1 will be suspended
3. When APP3 is enabled, APP2 will be suspended, and the same app will be suspended
4. When memory is limited, the APP1 will be terminated, but the user will not be notified
5. App2 immediately resumes from the suspended state after the user has enabled APP2
Activated introduction
1. Activated event is triggered when the app is activated
The 2.windows.ui.xaml.application class defines methods that can be overridden to handle a variety of different activation types, such as: Onactivated and onlaunched
3. The app should load its saved application data and refresh the content it displays
Suspend introduction
1. System resources focus on user interaction in the foreground application
2. Suspended apps will not consume any CPU, disk, and network resources
3. Inactive apps have no effect on the battery, they are suspended by the system
4. Fast switching between applications
5. Pending apps are saved in memory
6. All threads in the pending app are suspended
Terminated introduction
1. System needs more memory trigger
2. User Switching Trigger
3. User Shutdown App Trigger
4. System Shutdown Trigger
5. Application Crash trigger
6. No events are triggered during the termination of the application ... You should save all the information when the app hangs
Second, realize the activation application
When an app is activated, the system sends Activated events for whatever reason. For a list of activation types, refer to the Activationkind enumeration.
The Windows.UI.Xaml.Application class defines some methods that can be overridden to handle a variety of different activation types.
For some of these activation types, there are specific methods that can be overridden. For other activation types, the OnActivated method is overridden.
Suspending an app
1. The system hangs your app whenever the user switches to the desktop or another app. Every time the user cuts back to your app, the system resumes your app. When the system resumes your app, the contents of your variables and data structures are the same as before the system suspends your app. The system will completely restore your app to the state you left, making the user feel like your app is running in the background.
2. When your app is suspended, the system will try to keep your app and its data in memory. However, if the system does not have resources to keep your app in memory, your app will be terminated. When a user switches back to a suspended app that has been terminated, the app sends the Activated event, and its application data should be restored in its OnLaunched method.
3. When the app is terminated, the app is not notified, so when the app is suspended, your app must save its application data and release exclusive resources and file handles, and restore the content when the app is activated after termination.
Recovery application
1. The system hangs your app whenever the user switches to the desktop or another app. Every time the user cuts back to your app, the system resumes your app. When the system resumes your app, the contents of your variables and data structures are the same as before the system suspends your app. The system will completely restore your app to the state you left, making the user feel like your app is running in the background. However, the app may have been suspended for a long time, so it should refresh any display that might have changed after the app was suspended (such as a newsfeed or user location).
2. If your app doesn't have any display to refresh, it doesn't need to handle the Resuming event.
Third, the best practice must be implemented
1. In general, restore the app to the state when the user leaves the app, and do not restart
2. If it has been a long time since the user last accessed the app, restart the app
3. Save application data when app is suspended
4. Release exclusive resources and file handles when the app is suspended
5. If the app resumes after the app is suspended, update the UI if its content has changed since the user last viewed
6. Restore your app with saved application data when the app is restored after it's terminated
7. If you cannot predict whether the user wants to resume or restart, provide them with the option
Prohibited items
1. Do not terminate the app when the app is moved out of the screen
2. The status of an application that terminates without causing a crash
3. Do not provide a way for users to close or terminate apps in the app's UI
What should be done in different scenarios
Related references:
1. Manage the app lifecycle and status (Windows store apps using C#/VB and XAML);
2. How to activate the app;
3. How to suspend the application;
4. How to restore the application;
5. Application life cycle (Windows Store app) (Windows);
6. App's suspend and resume Guide (Windows Store app) (Windows);
Quickly build Windows 8 style Apps 30-App lifecycle management