Reputation: 1102
I'm working on inactivity detection.
I have successfully done so in iOS by subclassing UIApplication
and overriding SendEvent
as outlined here.
I know I could implement this separately for both iOS and Android, but I'd rather have a cross-platform Forms approach by intercepting all touch events and resetting my timers. I'd rather not have to add a touch event handler to all my pages either.
Upvotes: 2
Views: 1386
Reputation: 1102
I was unable to find a cross platform approach. I was able to accomplish this by leaving the timer related information in core Forms project and implement the touch event handlers separately for iOS and Android. I handled the iOS touch events as outlined in the link in the OP, but for Android I took the approach of subclassing Activity
due to the presence of the OnUserInteraction()
method.
Initially I thought I would have to force Xamarin to use my subclassed Activity
for all pages that I use in Xamarin, but I was mistaken. AdamMeaney over on the Xamarin forums was able to help with providing a solution for the Android side of things with regards to subclassing an Android activity. As it turns out, Xamarin only uses one Activity
which inherits from Xamarin.Forms.Platform.Android.FormsAppCompatActivity
. I used the MainAcitivty
provided by Xamarin in the Droid project. From there, overriding the OnUserInteraction()
proved to be quite simple:
public override void OnUserInteraction()
{
base.OnUserInteraction();
//Do other stuff
}
Upvotes: 3
Reputation: 6088
It would seem to me that all you really have to do on the platform side is get notified whenever a new touch event occurs. Unless I am missing something it seems you can do all of the timer stuff in the PCL core Forms project and call that code from the platform specific code that runs when a touch is detected.
So if on Android ( I did not verify, but I would assume so) there is a similar way to handle any touch, device wide, then it would seem that all you have to do is implement that event handler, as you did for iOS, and call into your Forms core code to handle the timer(s).
To clarify: On the platform side, just handle the touch events globally and then call into code in the Forms core thus only having to implement the timer functionality once. Or so it would seem unless I am missing something.
If you want to make a feature request for Xamarin Form, please do so at Xamarin's user voice page: xamarin.uservoice.com
I suspect Forms would just have to do as outlined above... handle device wide touches on each platform and then have a virtual method in Forms core code that is called whenever a touch occurs.
Upvotes: 0