Xamarin.Android 架构组件 : Not getting callbacks for lifecycle events

标签 xamarin xamarin.android 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.");
        }
    }
}

我的 Xamarin 版本是 8.2.0.16(Visual Studio Community),Xamarin.Android.Arch.Lifecycle.Extensions 版本是 1.0.0。我正在使用牛轧糖设备 (7.0) 进行测试。

最佳答案

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" />

这是在 Visual Studio 中的样子:

Required packages

为了能够设置生命周期观察者,我们需要一个生命周期所有者。在应用程序级别,这可以是 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.");
        }
    }
}

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

How to include 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());
        }
    }
}

关于Xamarin.Android 架构组件 : Not getting callbacks for lifecycle events,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49644709/

相关文章:

android - 如何用 Android Room 表示嵌套的@Relation?

c# - 如何根据条件更改 ListView 中的 Textcoroul 或 TextCell 的背景颜色

android - 引用 Mono.Android.Google map 时未安装应用程序错误

c# - 应用程序崩溃,NSInvalidLayoutConstraintException,iOS Storyboard设计器ViewController中的红色感叹号

ios - 如何使用第三方应用程序在 iPad 上实现分屏多任务处理?

c# - Android 上的 Xamarin.Forms TapRecognizer 取消 ItemTapped 事件

android - 房间删除​​查询不会删除数据库中的行

android - 如何有效地使用 android-architecture-components?

c# - 等待 MoveTo 操作完成的正确方法是什么?

android 中的 mvvmcross touch 命令绑定(bind)