Implemented MvxLifecycleCallbacksBasedAndroidCurrentTopActivity

Simplify MvxAndroidApplication
Edited MvxAndroidSetup: Fix register monitor
This commit is contained in:
nmilcoff 2018-01-06 21:01:20 -03:00
Родитель 7df1b831eb
Коммит fe098b9a69
3 изменённых файлов: 106 добавлений и 45 удалений

Просмотреть файл

@ -98,7 +98,7 @@ namespace MvvmCross.Droid.Platform
}
else
{
return new MvxMonitorBasedAndroidCurrentTopActivity();
return new MvxMonitorBasedAndroidCurrentTopActivity(Mvx.GetSingleton<IMvxAndroidActivityLifetimeListener>());
}
}

Просмотреть файл

@ -1,58 +1,21 @@
using System;
using Android.App;
using Android.OS;
using Android.Runtime;
namespace MvvmCross.Droid.Views
{
public class MvxAndroidApplication : Application, IMvxAndroidApplication, Application.IActivityLifecycleCallbacks
public class MvxAndroidApplication : Application, IMvxAndroidApplication
{
public static MvxAndroidApplication Instance { get; private set; }
public MvxAndroidApplication()
{
Instance = this;
}
public MvxAndroidApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public override void OnCreate()
{
base.OnCreate();
RegisterActivityLifecycleCallbacks(this);
}
public override void OnTerminate()
{
base.OnTerminate();
UnregisterActivityLifecycleCallbacks(this);
}
public void OnActivityCreated(Activity activity, Bundle savedInstanceState)
{
}
public void OnActivityDestroyed(Activity activity)
{
}
public void OnActivityPaused(Activity activity)
{
}
public void OnActivityResumed(Activity activity)
{
}
public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
{
}
public void OnActivityStarted(Activity activity)
{
}
public void OnActivityStopped(Activity activity)
{
Instance = this;
}
}
}

Просмотреть файл

@ -1,10 +1,108 @@
using System;
using System.Collections.Concurrent;
using Android.App;
using Android.OS;
using MvvmCross.Platform.Droid.Platform;
namespace MvvmCross.Droid.Views
{
public class MvxLifecycleCallbacksBasedAndroidCurrentTopActivity
public class MvxLifecycleCallbacksBasedAndroidCurrentTopActivity : Java.Lang.Object, Application.IActivityLifecycleCallbacks, IMvxAndroidCurrentTopActivity
{
public MvxLifecycleCallbacksBasedAndroidCurrentTopActivity()
private ConcurrentDictionary<string, ActivityInfo> _Activities = new ConcurrentDictionary<string, ActivityInfo>();
public Activity Activity => GetCurrentActivity();
public void OnActivityCreated(Activity activity, Bundle savedInstanceState)
{
var activityName = GetActivityName(activity);
_Activities.GetOrAdd(activityName, new ActivityInfo { Activity = activity, IsCurrent = true });
}
public void OnActivityDestroyed(Activity activity)
{
var activityName = GetActivityName(activity);
_Activities.TryRemove(activityName, out ActivityInfo removed);
}
public void OnActivityPaused(Activity activity)
{
ActivityInfo toAdd = new ActivityInfo { Activity = activity, IsCurrent = false };
var activityName = GetActivityName(activity);
_Activities.AddOrUpdate(activityName, toAdd, (key, existing) =>
{
existing.Activity = activity;
existing.IsCurrent = false;
return existing;
});
}
public void OnActivityResumed(Activity activity)
{
ActivityInfo toAdd = new ActivityInfo { Activity = activity, IsCurrent = true };
var activityName = GetActivityName(activity);
_Activities.AddOrUpdate(activityName, toAdd, (key, existing) =>
{
existing.Activity = activity;
existing.IsCurrent = true;
return existing;
});
}
public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
{
}
public void OnActivityStarted(Activity activity)
{
ActivityInfo toAdd = new ActivityInfo { Activity = activity, IsCurrent = true };
var activityName = GetActivityName(activity);
_Activities.AddOrUpdate(activityName, toAdd, (key, existing) =>
{
existing.Activity = activity;
existing.IsCurrent = true;
return existing;
});
}
public void OnActivityStopped(Activity activity)
{
ActivityInfo toAdd = new ActivityInfo { Activity = activity, IsCurrent = false };
var activityName = GetActivityName(activity);
_Activities.AddOrUpdate(activityName, toAdd, (key, existing) =>
{
existing.Activity = activity;
existing.IsCurrent = false;
return existing;
});
}
private Activity GetCurrentActivity()
{
if (_Activities.Count > 0)
{
var e = _Activities.GetEnumerator();
while (e.MoveNext())
{
var current = e.Current;
if (current.Value.IsCurrent)
{
return current.Value.Activity;
}
}
}
return null;
}
protected string GetActivityName(Activity activity) => activity.Class.SimpleName;
/// <summary>
/// Used to store additional info along with an activity.
/// </summary>
private class ActivityInfo
{
public bool IsCurrent { get; set; }
public Activity Activity { get; set; }
}
}
}