xamarin / AndroidSupportComponents

Xamarin bindings for Android Support libraries - For AndroidX see https://github.com/xamarin/AndroidX
MIT License
146 stars 56 forks source link

Unable to capture lifecycle events #99

Closed adienthu closed 5 years ago

adienthu commented 6 years ago

Xamarin.Android Version:

8.2.0.16 (Visual Studio Community)

Operating System & Version:

Mac OS X 10.13.1

Support Libraries Version:

27.0.2

Describe your Issue:

I'm unable to get the capture lifecycle events to detect application entering background or foreground. In the sample code below, the methods onApplicationForegrounded and onApplicationBackgrounded are not invoked:

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.");
        }
    }
}

VS bug #732431

erikpowa commented 6 years ago

Same here, can't trigger lifecycle event on a MapView. Annotations are probably broken.

ocjadan commented 6 years ago

Add [Export] and it’ll work

[Lifecycle.Event.OnStart] [Export] public void onAppForegrounded() { Log.Debug(TAG, "App entered foreground state."); }

erikpowa commented 6 years ago

@ocjadan ty, but I still get Java.Lang.IllegalArgumentException: invalid parameter type. Must be one and instanceof LifecycleOwner at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <f32579baafc1404fa37ba3ec1abdc0bd>:0 at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <09bf3e262b934ffab2ba01f9fc7fd54d>:0 at Android.Runtime.JNIEnv.CallVoidMethod (System.IntPtr jobject, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x0000e] in <25661073a35344a89f215a4cf81af37c>:0 when I try to add an Observer to a Owner

ReflectiveGenericLifecycleObserver.java if (params.length > 0) { callType = CALL_TYPE_PROVIDER; if (!params[0].isAssignableFrom(LifecycleOwner.class)) { throw new IllegalArgumentException( "invalid parameter type. Must be one and instanceof LifecycleOwner"); } }

edit: I added LifecycleOwner as first parameter in Observer's callbacks and it's not crying about it anymore but the Android doc says:

Observer methods can receive zero or one argument. If used, the first argument must be of type LifecycleOwner. Methods annotated with ON_ANY can receive the second argument, which must be of type Lifecycle.Event.

Redth commented 5 years ago

We'll take a look at this but I suspect we haven't allowed for arguments as you seem to have found.

Redth commented 5 years ago

The code we're using really only does some trickery around emitting the correct java annotation. As long as you're using [Export] I don't see why this shouldn't work to use the allowed zero or one argument as per the rules in the docs.