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.
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
}
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.
Related
I just checked out consuming native UI iOS/Android controls in Xamarin Forms. One thing I don't understand is what events to specify as triggering an update for bindings. The article mentions UISwitch as an example and specifies the ValueChanged event for triggering updating bindings. But where can I find information about which events can be used for other iOS and Android controls?
As an example I tried UITextView and couldn't find any info on which events is being supported in the official docs. Some experimenting and I found I could use an event called "Changed", but there has to be some docs somewhere for this I guess. Antone know where?
iOS uses the delegate pattern heavily. UITextView uses the UITextViewDelegate - in native iOS you would provide your own delegate that is responsible for responding to events raised by the control
I've found that whilst there are a lot of tutorials on Xamarin Android, there does not seem to be a great deal on how to dispose of resources. More particularly, when they are disposed of.
For example, in the OnCreate handler of an activity, I am making several Rx subscriptions, each of which returns an IDisposable. I have tried to dispose of those in various other handlers (e.g. OnDestroy), but those handlers never get invoked. But the subscriptions seem to pile up because OnCreate runs every time the activity is navigated to.
In addition to those subscriptions, there's all the UI controls (TextViews, Buttons etc.) which I am assigning to class-level variables (fields). And those also implement IDisposable.
For all I know, I've got memory leaks all over the place.
Is there a guidance on this anywhere?
#SushiHangover is correct (thanks Sushi). OnPause and OnResume were the events I was after. I also had a bit of a challenge in that when I clicked my custom "Back to Start" button, I needed to go right back to the start screen (skipping the intermediate screen along the way).
The way to do that is use the ClearTop ActivityFlag (Android.Content.ActivityFlags.ClearTop) when starting the Home screen activity. Raw Android code version of this can be seen here https://stackoverflow.com/a/5794572/540156
When you do that, you can clean things up on the activities which get popped off the back-stack as they get popped (in the OnDestroy handler, from recollection).
As per the requirement of the project I need to have the page which should behave as the default Apple Mail app on the iPhone.
Attaching a gif that I have created for the same.
I tried using the plug in https://github.com/rotorgames/Rg.Plugins.Popup
but I couldn't achieve the exact same behavior where the background shrinks and expands as the modal pop up is dragged.Any leads, examples or demos will be really helpful.
Note: I am using Xamarin.Forms.
With the new Xamain Forms update. i.e. after iOS13
The model behavior has became the default model behavior for Xamarin iOS.
When you do.
Navigation.PushModalAsyn(<your_page_here>).
It gives the above (gif) behavior.
I using scnViewGesture.Plugin for xamarin forms. LongTap is working as expected in iOS, but in the Android the LongTap event is not working. please share your inputs.
Why do you need a plugin to do this? Just handle the touch event, start a timer on the down event, clear the timer on the up event, and whenever a specified amount of time has passed on the timer, do whatever you wanted to do with the long touch on a separate thread.
For the record, I can't give you a better example because you've provided no code, and your question is extremely broad because of that.
I'm pretty new to Appcelerator and was wondering what the listeners for the view life cycle are?
For example, if I wanted to detect the iOS viewWillAppear and viewDidDisappear methods, or Androids OnResume, OnPause methods, then how would I do this the "Appcelerator" way?
I've searched around on the web, but only able to find in the Titanium documentation info about the application state such as Active, Suspended, ect. I need a controller, or window, specific listener to react to.
Thanks!
Titanium abstracts those events for you - so you don't have to worry about writing them for iOS/Android each.
Check out the Titanium.App documentation (http://docs.appcelerator.com/platform/latest/#!/api/Titanium.App). You can see what events are available at the app level (of course, each Titanium components has it's own events - but those are at app level).
If I understand your question, the relevant events for you are paused and resumed - when the app goes in the background and then back to foreground.