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

时间:2018-04-04 06:59:49

标签: 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社区),Xamarin.Android.Arch.Lifecycle.Extensions版本是1.0.0。我使用Nougat设备(7.0)进行测试。

3 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:0)

如果您在活动中需要它,那么事件:

protected override void OnStart(){
            base.OnStart();
            Log.Debug(logTag, "MainActivity.OnStart() called, the activitiy is active");
        }

        protected override void OnPause()
        {
            base.OnPause();
            Log.Debug(logTag, "MainActivity.OnPause() called, the activity in background");
        }

        protected override void OnStop()
        {
            base.OnStop();
            Log.Debug(logTag, "MainActivity.OnStop() called, the activity is in background because of other activiy or app");
        }

        protected override void OnResume()
        {
            base.OnResume();
            Log.Debug(logTag, "MainActivity.OnResume() called, the activity stated");
        }

        protected override void OnRestart()
        {
            base.OnRestart();
            Log.Debug(logTag, "MainActivity.OnRestart() called,  the activity is startet");
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();
            Log.Debug(logTag, "MainActivity.OnDestroy() called, activity is destroyed");
        }

对于Xamarin Forms,您将在app.xaml.cs中找到应用程序所需的事件。

protected override void OnStart ( ) {
            // Handle when your app starts
        }

        protected override void OnSleep ( ) {
            // Handle when your app sleeps
        }

        protected override void OnResume ( ) {
            // Handle when your app resumes
        }

答案 2 :(得分:0)

我过去曾使用过该软件包,但我更喜欢James Montemagno的实现,它可以作为名为“Plugin.CurrentActivity”的nuget软件包找到。它创建一个应用程序类并为您实现ILifecycle事件。

来自说明:

  

提供一个简单的解决方案,用于在为Xamarin开发插件时访问应用程序的当前Activity。            这将为Android应用程序中的开发人员提供一个基础“应用程序”类,其中包含用于启动它们的样板代码。            可以与Android API 14 +一起使用


*我假设您没有使用Xamarin.Forms。这适用于原生Xamarin Android项目。

Link to the Github page