Skip to content

Commit

Permalink
Fix Android Top Activity (MvvmCross#4290)
Browse files Browse the repository at this point in the history
* Fix Android Top Activity, old code was overcomplicated

* Fix layouts using old types
  • Loading branch information
Cheesebaron authored Oct 14, 2021
1 parent 2e4cc87 commit 829ceb6
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected override void OnCreate(Bundle bundle)
{
// ensuring mvvmcross is running here is required
// otherwise app will crash when inflating the view because of the Forms base class
var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(ApplicationContext);
var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(this);
setup.EnsureInitialized();

base.OnCreate(bundle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected override void OnCreate(Bundle bundle)
{
// ensuring mvvmcross is running here is required
// otherwise app will crash when inflating the view because of the Forms base class
var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(ApplicationContext);
var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(this);
setup.EnsureInitialized();

base.OnCreate(bundle);
Expand Down
8 changes: 5 additions & 3 deletions MvvmCross/Platforms/Android/Core/IMvxAndroidSetup.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MS-PL license.
// See the LICENSE file in the project root for more information.

using Android.Content;
using Android.App;
using MvvmCross.Core;

namespace MvvmCross.Platforms.Android.Core
{
#nullable enable
public interface IMvxAndroidSetup : IMvxSetup
{
void PlatformInitialize(Context applicationContext);
void PlatformInitialize(Activity activity);
}
#nullable restore
}
44 changes: 25 additions & 19 deletions MvvmCross/Platforms/Android/Core/MvxAndroidSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,40 @@
using MvvmCross.Views;
using MvvmCross.Presenters;
using System.Linq;
using Android.App;

namespace MvvmCross.Platforms.Android.Core
{
#nullable enable
public abstract class MvxAndroidSetup
: MvxSetup, IMvxAndroidGlobals, IMvxAndroidSetup
{
private Context? _applicationContext;
private MvxCurrentTopActivity? _currentTopActivity;
private IMvxAndroidViewPresenter? _presenter;

public void PlatformInitialize(Context applicationContext)
public void PlatformInitialize(Activity activity)
{
if (applicationContext == null)
throw new ArgumentNullException(nameof(applicationContext));
if (activity == null)
throw new ArgumentNullException(nameof(activity));

_applicationContext = applicationContext;
PlatformInitialize(activity.Application);
}

public void PlatformInitialize(Application application)
{
if (application == null)
throw new ArgumentNullException(nameof(application));

if (_currentTopActivity != null)
return;

_currentTopActivity = new MvxCurrentTopActivity();
application?.RegisterActivityLifecycleCallbacks(_currentTopActivity);
}

public virtual Assembly ExecutableAssembly => ViewAssemblies?.FirstOrDefault() ?? GetType().Assembly;

public Context? ApplicationContext => _applicationContext;
public Context? ApplicationContext => _currentTopActivity?.Activity?.ApplicationContext;

protected override void InitializeFirstChance(IMvxIoCProvider iocProvider)
{
Expand Down Expand Up @@ -78,17 +91,10 @@ protected virtual void InitializeAndroidCurrentTopActivity(IMvxIoCProvider iocPr

protected virtual IMvxAndroidCurrentTopActivity CreateAndroidCurrentTopActivity()
{
var mvxApplication = MvxAndroidApplication.Instance;
if (mvxApplication != null)
{
var activityLifecycleCallbacksManager = new MvxApplicationCallbacksCurrentTopActivity();
mvxApplication.RegisterActivityLifecycleCallbacks(activityLifecycleCallbacksManager);
return activityLifecycleCallbacksManager;
}
else
{
return new MvxLifecycleMonitorCurrentTopActivity(Mvx.IoCProvider.GetSingleton<IMvxAndroidActivityLifetimeListener>());
}
if (_currentTopActivity == null)
throw new InvalidOperationException($"Please call {nameof(PlatformInitialize)} first");

return _currentTopActivity;
}

protected virtual void InitializeLifetimeMonitor(IMvxIoCProvider iocProvider)
Expand Down Expand Up @@ -123,10 +129,10 @@ protected sealed override IMvxViewsContainer CreateViewsContainer(IMvxIoCProvide
{
ValidateArguments(iocProvider);

if (_applicationContext == null)
if (ApplicationContext == null)
throw new InvalidOperationException("Cannot create Views Container without ApplicationContext");

var container = CreateViewsContainer(_applicationContext);
var container = CreateViewsContainer(ApplicationContext);
iocProvider.RegisterSingleton<IMvxAndroidViewModelRequestTranslator>(container);
iocProvider.RegisterSingleton<IMvxAndroidViewModelLoader>(container);
var viewsContainer = container as MvxViewsContainer;
Expand Down
10 changes: 9 additions & 1 deletion MvvmCross/Platforms/Android/Core/MvxAndroidSetupSingleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MS-PL license.
// See the LICENSE file in the project root for more information.

using Android.App;
using Android.Content;
using MvvmCross.Core;

Expand All @@ -10,10 +11,17 @@ namespace MvvmCross.Platforms.Android.Core
public class MvxAndroidSetupSingleton
: MvxSetupSingleton
{
public static MvxAndroidSetupSingleton EnsureSingletonAvailable(Activity application)
{
var instance = EnsureSingletonAvailable<MvxAndroidSetupSingleton>();
instance.PlatformSetup<MvxAndroidSetup>()?.PlatformInitialize(application);
return instance;
}

public static MvxAndroidSetupSingleton EnsureSingletonAvailable(Context applicationContext)
{
var instance = EnsureSingletonAvailable<MvxAndroidSetupSingleton>();
instance.PlatformSetup<MvxAndroidSetup>()?.PlatformInitialize(applicationContext);
instance.PlatformSetup<MvxAndroidSetup>()?.PlatformInitialize(applicationContext as Application);
return instance;
}
}
Expand Down
3 changes: 1 addition & 2 deletions MvvmCross/Platforms/Android/Services/MvxBroadcastReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using Android.Content;
using Android.Runtime;
using MvvmCross.Core;
using MvvmCross.Platforms.Android.Core;

namespace MvvmCross.Platforms.Android.Services
Expand All @@ -24,7 +23,7 @@ protected MvxBroadcastReceiver()

public override void OnReceive(Context context, Intent intent)
{
var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(context);
var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(context.ApplicationContext);
setup.EnsureInitialized();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private static void EnsureSetupInitialized(this IMvxAndroidView androidView)
}

var activity = androidView.ToActivity();
var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(activity.ApplicationContext);
var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(activity);
setup.EnsureInitialized();
}
}
Expand Down

This file was deleted.

60 changes: 60 additions & 0 deletions MvvmCross/Platforms/Android/Views/MvxCurrentTopActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MS-PL license.
// See the LICENSE file in the project root for more information.

using System;
using Android.App;
using Android.OS;
using Android.Runtime;

namespace MvvmCross.Platforms.Android.Views
{
#nullable enable
[Register("mvvmcross.platforms.android.MvxCurrentTopActivity")]
public class MvxCurrentTopActivity
: Java.Lang.Object, Application.IActivityLifecycleCallbacks, IMvxAndroidCurrentTopActivity
{
[Weak]
private Activity? _lastSeenActivity;

public Activity? Activity => _lastSeenActivity;

public static bool Initialized { get; set; }

public void OnActivityCreated(Activity activity, Bundle? savedInstanceState)
{
_lastSeenActivity = activity;
}

public void OnActivityPaused(Activity activity)
{
_lastSeenActivity = activity;
}

public void OnActivityResumed(Activity activity)
{
_lastSeenActivity = activity;
}

public void OnActivityDestroyed(Activity activity)
{
// not interested in this
}

public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
{
// not interested in this
}

public void OnActivityStarted(Activity activity)
{
// not interested in this
}

public void OnActivityStopped(Activity activity)
{
// not interested in this
}
}
#nullable restore
}

This file was deleted.

Loading

0 comments on commit 829ceb6

Please sign in to comment.