Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pending lifecycle events hooks #7225

Merged
merged 1 commit into from May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Controls/samples/Controls.Sample/MauiWindow.cs
@@ -0,0 +1,35 @@
using System;
using Microsoft.Maui.Controls;

namespace Maui.Controls.Sample
{
public class MauiWindow : Window
{
public MauiWindow() : base() { }
public MauiWindow(Page page) : base(page) { }

protected override void OnCreated()
{
Console.WriteLine("OnCreated");
base.OnCreated();
}

protected override void OnStopped()
{
Console.WriteLine("OnStopped");
base.OnStopped();
}

protected override void OnResumed()
{
Console.WriteLine("OnResumed");
base.OnResumed();
}

protected override void OnDestroying()
{
Console.WriteLine("OnDestroying");
base.OnDestroying();
}
}
}
8 changes: 5 additions & 3 deletions src/Controls/samples/Controls.Sample/XamlApp.xaml.cs
Expand Up @@ -5,7 +5,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Storage;

namespace Maui.Controls.Sample
Expand Down Expand Up @@ -49,8 +48,11 @@ async void LoadAsset()
// Must not use MainPage for multi-window
protected override Window CreateWindow(IActivationState activationState)
{
var window = new Window(Services.GetRequiredService<Page>());
window.Title = ".NET MAUI Samples Gallery";
var window = new MauiWindow(Services.GetRequiredService<Page>())
{
Title = ".NET MAUI Samples Gallery"
};

return window;
}

Expand Down
@@ -1,6 +1,4 @@
using System;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;

namespace Microsoft.Maui.LifecycleEvents
{
Expand Down Expand Up @@ -66,4 +64,4 @@ static void OnConfigureLifeCycle(IAndroidLifecycleBuilder android)
});
}
}
}
}
@@ -1,9 +1,7 @@
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;
using System;

namespace Microsoft.Maui.LifecycleEvents
{
{
public static partial class AppHostBuilderExtensions
{
internal static MauiAppBuilder ConfigureCrossPlatformLifecycleEvents(this MauiAppBuilder builder) =>
Expand Down
Expand Up @@ -3,7 +3,6 @@
using Android.Content.PM;
using Android.Content.Res;
using Android.OS;
using Android.Views;
using Microsoft.Maui.Devices;
using Microsoft.Maui.LifecycleEvents;

Expand Down Expand Up @@ -95,5 +94,12 @@ protected override void OnRestoreInstanceState(Bundle savedInstanceState)

MauiApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnRestoreInstanceState>(del => del(this, savedInstanceState));
}

protected override void OnDestroy()
{
base.OnDestroy();

MauiApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnDestroy>(del => del(this));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note from the google docs:
https://developer.android.com/reference/android/app/Activity#onDestroy()

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

}
}
}
6 changes: 6 additions & 0 deletions src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs
Expand Up @@ -114,6 +114,12 @@ public virtual void OnResignActivation(UIApplication application)
Services?.InvokeLifecycleEvents<iOSLifecycle.OnResignActivation>(del => del(application));
}

[Export("sceneDidDisconnect:")]
public void DidDisconnect(UIScene scene)
{
Services?.InvokeLifecycleEvents<iOSLifecycle.SceneDidDisconnect>(del => del(scene));
}

[Export("applicationWillTerminate:")]
public virtual void WillTerminate(UIApplication application)
{
Expand Down