| Start |
|---|
| Application lifecycle First of all you should know that you can extend Android.Application class so you can access two important methods related with app lifecycle: OnTerminate - This method is for use in emulated process environments. It will never be called on a production Android device, where processes are removed by simply killing them; No user code (including this callback) is executed when doing so. From the documentation: [https://developer.android.com/reference/android/app/Application.html#onTerminate()](https://developer.android.com/reference/android/app/Application.html#onTerminate()) In Xamarin.Android application you can extend Application class in the way presented below. Add new class called "MyApplication.cs" to your project: As you wrote above you can use OnCreate method. You can for instance initialize local database here or setup some additional configuration. There is also more methods which can be overridden like: OnConfigurationChanged or OnLowMemory. |
| Activity lifecycle Activity lifecycle is quite more complex. As you know Activity is single page in the Android app where user can perform interaction with it. On the diagram below you can see how Android Activity lifecycle looks like: As you can see there is specific flow of Activity lifecycle. In the mobile application you have of course methods in each Activity class that handle specific lifecycle fragment: There is good description in the official Android documentation: |
| Fragment lifecycle As you know you can have one activity but different fragments embedded in it. That is why fragment lifecycle is also important for developers. On the diagram below you can see how Android fragment lifecycle looks like: As described in the official Android documentation you should implement at least below three methods: Here is sample implementation in Xamarin.Android: Of course you can add additional methods here if you want to handle different states. |
| Full sample on GitHub If you would like to get base project with methods described below you can download Xamarin.Android application template from my GitHub. You can find examples for:
RemarksInteresting links to broad your knowledge about Android application lifecycle: https://developer.android.com/reference/android/app/Activity.html (opens new window) http://www.vogella.com/tutorials/AndroidLifeCycle/article.html (opens new window) https://github.com/xxv/android-lifecycle (opens new window) https://developer.android.com/guide/components/fragments.html (opens new window) https://developer.android.com/guide/components/activities/activity-lifecycle.html (opens new window) |
Read Article
https://develop.horje.com/learn/1434/reference