Xamarin.Android架构组件:没有得到生命周期事件的回调

adi*_*thu 11 xamarin.android xamarin android-architecture-components

我正在尝试使用体系结构组件包来检测应用程序何时进入后台或前台状态.问题是没有调用回调.在以下示例代码,所述方法onApplicationForegroundedonApplicationBackgrounded不调用:

namespace POC.Droid
{
    [Application]
    public class MyApp : Application, ILifecycleObserver
    {
        static readonly string TAG = "MyApp";

        public MyApp(IntPtr handle, Android.Runtime.JniHandleOwnership ownerShip) : base(handle, ownerShip)
        {
        }

        public override void OnCreate()
        {
            base.OnCreate();
            ProcessLifecycleOwner.Get().Lifecycle.AddObserver(this);
        }

        [Lifecycle.Event.OnStop]
        public void onAppBackgrounded()
        {
            Log.Debug(TAG, "App entered background state.");
        }

        [Lifecycle.Event.OnStart]
        public void onAppForegrounded()
        {
            Log.Debug(TAG, "App entered foreground state.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的Xamarin版本是8.2.0.16(Visual Studio社区),Xamarin.Android.Arch.Lifecycle.Extensions版本是1.0.0.我正在使用Nougat设备(7.0)进行测试.

Tho*_*mas 11

TL; DR请用你的生命周期回调注释 [Export]

这里有更详细的描述:

通常,要调用生命周期观察器的方法,请确保存在相关的包.这是我的packages.config的一部分:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Xamarin.Android.Arch.Core.Common" version="26.1.0" targetFramework="monoandroid81" />
  <package id="Xamarin.Android.Arch.Core.Runtime" version="1.0.0.1" targetFramework="monoandroid81" />
  <package id="Xamarin.Android.Arch.Lifecycle.Common" version="26.1.0" targetFramework="monoandroid81" />
  <package id="Xamarin.Android.Arch.Lifecycle.Extensions" version="1.0.0.1" targetFramework="monoandroid81" />
  <package id="Xamarin.Android.Arch.Lifecycle.Runtime" version="1.0.3.1" targetFramework="monoandroid81" />
Run Code Online (Sandbox Code Playgroud)

这是在Visual Studio中的外观:

必需的包

为了能够设置生命周期观察者,我们需要一个生命周期所有者.在应用程序级别上,这可以ProcessLifecycleOwner像原始海报所示.

这是一个稍微修改过的版本:

using System;
using Android.App;
using Android.Arch.Lifecycle;
using Android.Util;
using Java.Interop;

namespace Stopwatch_AAC
{
    [Application]
    public class MyApp : Application, ILifecycleObserver
    {
        const string TAG = "MyApp";

        public MyApp(IntPtr handle, Android.Runtime.JniHandleOwnership ownerShip) : base(handle, ownerShip)
        {
        }

        public override void OnCreate()
        {
            base.OnCreate();
            ProcessLifecycleOwner.Get().Lifecycle.AddObserver(this);
        }

        [Lifecycle.Event.OnStop]
        [Export]
        public void Stopped()
        {
            Log.Debug(TAG, "App entered background state.");
        }

        [Lifecycle.Event.OnStart]
        [Export]
        public void Started()
        {
            Log.Debug(TAG, "App entered foreground state.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,您可以使用例如注释生命周期方法[Lifecycle.Event.OnStop].另外,请注意您需要使用[Export].请确保在项目中引用Mono.Android.Export,如以下屏幕截图所示.

如何包含Mono.Android.Export

如果你想拥有一个活动的生命周期观察者,我建议扩展,AppCompatActivity因为它是一个生命周期所有者:

using Android.App;
using Android.Arch.Lifecycle;
using Android.OS;
using Android.Support.V7.App;
using Android.Util;
using Java.Interop;

namespace Stopwatch_AAC
{

    [Activity(Label = "Minimal", Exported = true, MainLauncher = true)]
    public class Minimal : AppCompatActivity, ILifecycleObserver
    {
        const string TAG = "Stopwatch_AAC";

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Lifecycle.AddObserver(this);
            Log.Debug(TAG, Lifecycle.CurrentState.ToString());
        }

        [Lifecycle.Event.OnAny]
        [Export]
        public void Hello()
        {
            Log.Debug(TAG, Lifecycle.CurrentState.ToString());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)