Page Lifecycle Events on Android
This Android platform-specific is used to disable the Disappearing
and Appearing
page events on application pause and resume respectively, for applications that use AppCompat. In addition, it includes the ability to control whether the soft keyboard is displayed on resume, if it was displayed on pause, provided that the operating mode of the soft keyboard is set to WindowSoftInputModeAdjust.Resize
.
Note
Note that these events are enabled by default to preserve existing behavior for applications that rely on the events. Disabling these events makes the AppCompat event cycle match the pre-AppCompat event cycle.
This platform-specific can be consumed in XAML by setting the Application.SendDisappearingEventOnPause
, Application.SendAppearingEventOnResume
, and Application.ShouldPreserveKeyboardOnResume
attached properties to boolean
values:
<Application ...
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core" xmlns:androidAppCompat="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific.AppCompat;assembly=Xamarin.Forms.Core"
android:Application.WindowSoftInputModeAdjust="Resize"
androidAppCompat:Application.SendDisappearingEventOnPause="false"
androidAppCompat:Application.SendAppearingEventOnResume="false"
androidAppCompat:Application.ShouldPreserveKeyboardOnResume="true">
...
</Application>
Alternatively, it can be consumed from C# using the fluent API:
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific.AppCompat;
...
Xamarin.Forms.Application.Current.On<Android>()
.UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize)
.SendDisappearingEventOnPause(false)
.SendAppearingEventOnResume(false)
.ShouldPreserveKeyboardOnResume(true);
The Application.Current.On<Android>
method specifies that this platform-specific will only run on Android. The Application.SendDisappearingEventOnPause
method, in the Xamarin.Forms.PlatformConfiguration.AndroidSpecific.AppCompat
namespace, is used to enable or disable firing the Disappearing
page event, when the application enters the background. The Application.SendAppearingEventOnResume
method is used to enable or disable firing the Appearing
page event, when the application resumes from the background. The Application.ShouldPreserveKeyboardOnResume
method is used control whether the soft keyboard is displayed on resume, if it was displayed on pause, provided that the operating mode of the soft keyboard is set to WindowSoftInputModeAdjust.Resize
.
The result is that the Disappearing
and Appearing
page events won't be fired on application pause and resume respectively, and that if the soft keyboard was displayed when the application was paused, it will also be displayed when the application resumes: