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

Native lifecycle events #495

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .nuspec/Microsoft.Maui.Controls.MultiTargeting.targets
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
<ItemGroup Condition="$(TargetFramework.Contains('-windows')) != true ">
<Compile Remove="**\*.Windows.cs" />
<None Include="**\*.Windows.cs" />
<Compile Remove="**\*.xaml.cs" />
<None Include="**\*.xaml.cs" />
<Compile Remove="**\*.xaml" />
<None Include="**\*.xaml" />
<Compile Remove="**\*.Windows.xaml.cs" />
<None Include="**\*.Windows.xaml.cs" />
<Compile Remove="**\*.Windows.xaml" />
<None Include="**\*.Windows.xaml" />
<Compile Remove="**\Windows\*.cs" />
<None Include="**\Windows\*.cs" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/samples/Controls.Sample/Controls.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@
<MauiFont Include="Resources\Fonts\*" />
</ItemGroup>

<Import Project="..\..\..\..\.nuspec\Microsoft.Maui.Controls.MultiTargeting.targets" />

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Runtime.CompilerServices;
using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.OS;
using Microsoft.Maui;

namespace Maui.Controls.Sample.Services
{
public class CustomAndroidLifecycleHandler : AndroidApplicationLifetime
{
public override void OnCreate(Activity activity, Bundle savedInstanceState) => LogMember();

public override void OnPostCreate(Activity activity, Bundle savedInstanceState) => LogMember();

public override void OnDestroy(Activity activity) => LogMember();

public override void OnPause(Activity activity) => LogMember();

public override void OnResume(Activity activity) => LogMember();

public override void OnPostResume(Activity activity) => LogMember();

public override void OnStart(Activity activity) => LogMember();

public override void OnStop(Activity activity) => LogMember();

public override void OnRestart(Activity activity) => LogMember();

public override void OnSaveInstanceState(Activity activity, Bundle outState) => LogMember();

public override void OnRestoreInstanceState(Activity activity, Bundle savedInstanceState) => LogMember();

public override void OnConfigurationChanged(Activity activity, Configuration newConfig) => LogMember();

public override void OnActivityResult(Activity activity, int requestCode, Result resultCode, Intent data) => LogMember();

static void LogMember([CallerMemberName] string name = "") =>
System.Diagnostics.Debug.WriteLine("LIFECYCLE: " + name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Foundation;
using Microsoft.Maui;
using UIKit;

namespace Maui.Controls.Sample
{
public class CustomIosLifecycleHandler : IosApplicationLifetime
{
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
LogMember();

return true;
}

public override void OnActivated(UIApplication application) => LogMember();

public override void OnResignActivation(UIApplication application) => LogMember();

public override void WillTerminate(UIApplication application) => LogMember();

public override void DidEnterBackground(UIApplication application) => LogMember();

public override void WillEnterForeground(UIApplication application) => LogMember();

static void LogMember([CallerMemberName] string name = "") =>
Debug.WriteLine("LIFECYCLE: " + name);
}
}
12 changes: 9 additions & 3 deletions src/Controls/samples/Controls.Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public void Configure(IAppHostBuilder appBuilder)
{"Logging:LogLevel:Default", "Warning"}
});
})
.UseMauiServiceProviderFactory(true)
//.UseServiceProviderFactory(new DIExtensionsServiceProviderFactory())
//.UseMauiServiceProviderFactory(true)
.UseServiceProviderFactory(new DIExtensionsServiceProviderFactory())
Copy link
Member

Choose a reason for hiding this comment

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

We will need to see if there is a fast way to handle multiple service registration and if it will conflict with the single registration we want for things like handlers.

.ConfigureServices((hostingContext, services) =>
{
services.AddSingleton<ITextService, TextService>();
Expand All @@ -56,14 +56,20 @@ public void Configure(IAppHostBuilder appBuilder)
services.AddTransient<IPage, MainPage>();

services.AddTransient<IWindow, MainWindow>();

#if __ANDROID__
services.AddTransient<IAndroidApplicationLifetime, CustomAndroidLifecycleHandler>();
#elif __IOS__
services.AddTransient<IIosApplicationLifetime, CustomIosLifecycleHandler>();
#endif
})
.ConfigureFonts((hostingContext, fonts) =>
{
fonts.AddFont("Dokdo-Regular.ttf", "Dokdo");
});
}

// To use DI ServiceCollection and not the MAUI one
// To use the Microsoft.Extensions.DependencyInjection ServiceCollection and not the MAUI one
public class DIExtensionsServiceProviderFactory : IServiceProviderFactory<ServiceCollection>
{
public ServiceCollection CreateBuilder(IServiceCollection services)
Expand Down
81 changes: 81 additions & 0 deletions src/Core/src/Platform/Android/AndroidApplicationLifetime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.OS;
using Android.Runtime;

namespace Microsoft.Maui
{
public class AndroidApplicationLifetime : IAndroidApplicationLifetime
{
public virtual void OnCreate(Activity activity, Bundle? savedInstanceState)
{

}

public virtual void OnPostCreate(Activity activity, Bundle? savedInstanceState)
{

}

public virtual void OnDestroy(Activity activity)
{

}

public virtual void OnPause(Activity activity)
{

}

public virtual void OnResume(Activity activity)
{

}

public virtual void OnPostResume(Activity activity)
{

}

public virtual void OnStart(Activity activity)
{

}

public virtual void OnStop(Activity activity)
{

}

public virtual void OnRestart(Activity activity)
{

}

public virtual void OnSaveInstanceState(Activity activity, Bundle outState)
{

}

public virtual void OnRestoreInstanceState(Activity activity, Bundle savedInstanceState)
{

}

public virtual void OnConfigurationChanged(Activity activity, Configuration newConfig)
{

}

public virtual void OnActivityResult(Activity activity, int requestCode, [GeneratedEnum] Result resultCode, Intent? data)
{

}

public virtual void OnBackPressed(Activity activity)
{

}
}
}
111 changes: 111 additions & 0 deletions src/Core/src/Platform/Android/IAndroidApplicationLifetime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.OS;
using Android.Runtime;

namespace Microsoft.Maui
{
/// <summary>
/// Allow to get Android Activity lifecycle callbacks.
/// </summary>
public interface IAndroidApplicationLifetime : IPlatformApplicationLifetime
{
/// <summary>
/// Called when the activity is starting.
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks.</param>
/// <param name="savedInstanceState">Previous state saved.</param>
void OnCreate(Activity activity, Bundle? savedInstanceState);

/// <summary>
/// Called when activity start-up is complete (after OnStart() and OnRestoreInstanceState(Bundle) have been called).
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks.</param>
/// <param name="savedInstanceState">Previous state saved.</param>
void OnPostCreate(Activity activity, Bundle? savedInstanceState);

/// <summary>
/// Called when the activity had been stopped, but is now again being displayed to the user.
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks.</param>
void OnStart(Activity activity);

/// <summary>
/// Called for your activity to start interacting with the user.
/// This is an indicator that the activity became active and ready to receive input.
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks.</param>
void OnResume(Activity activity);

/// <summary>
/// Called when activity resume is complete (after OnResume() has been called).
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks.</param>
void OnPostResume(Activity activity);

/// <summary>
/// Called as part of the activity lifecycle when the user no longer actively interacts with the activity,
/// but it is still visible on screen.
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks.</param>
void OnPause(Activity activity);

/// <summary>
/// Called when you are no longer visible to the user.
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks</param>
void OnStop(Activity activity);

/// <summary>
/// Called after OnStop when the current activity is being re-displayed to the user (the user has navigated back to it).
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks</param>
void OnRestart(Activity activity);

/// <summary>
/// This can happen either because the activity is finishing, or because the system is
/// temporarily destroying this instance of the activity to save space.
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks.</param>
void OnDestroy(Activity activity);

/// <summary>
/// Called to retrieve per-instance state from an activity before being killed so that the state can be
/// restored in OnCreate or OnRestoreInstanceState.
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks.</param>
/// <param name="outState">Bundle in which to place your saved state.</param>
void OnSaveInstanceState(Activity activity, Bundle outState);

/// <summary>
/// Restore the state of the dialog from a previously saved bundle.
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks.</param>
/// <param name="savedInstanceState">The state of the dialog previously saved.</param>
void OnRestoreInstanceState(Activity activity, Bundle savedInstanceState);

/// <summary>
/// Called when the current configuration of the resources being used by the application have changed.
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks.</param>
/// <param name="newConfig">The new resource configuration.</param>
void OnConfigurationChanged(Activity activity, Configuration newConfig);

/// <summary>
/// Called when an activity you launched exits, giving you the requestCode you started it with,
/// the resultCode it returned, and any additional data from it.
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks.</param>
/// <param name="requestCode">The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from.</param>
/// <param name="resultCode">The integer result code returned by the child activity.</param>
/// <param name="data">An Intent, which can return result data to the caller.</param>
void OnActivityResult(Activity activity, int requestCode, [GeneratedEnum] Result resultCode, Intent? data);

/// <summary>
/// Called when the activity has detected the user's press of the back key.
/// </summary>
/// <param name="activity">The activity on which we receive lifecycle events callbacks.</param>
void OnBackPressed(Activity activity);
}
}
Loading