7. Android: Activities
An Activity
is like a…
◦ Windows Process
◦ ASP.NET Page Life Cycle
◦ MVC Page Life Cycle
8. Back vs. Home
Image courtesy of Xamarin
Back
•User is done with the Activity. Destroy it.
Home (or App Switcher)
•Suspend the Activity. Place in the background.
12. Bundle – for Activity State
Key/value dictionary
Bundle not null in OnCreate?
Restarting.
13. Activity Methods
OnStart - before an activity becomes visible
◦ refresh current values of views
OnResume - start interacting with the user
◦
◦
◦
◦
◦
◦
Ramping up frame rates
Starting animations
Listening for GPS updates
Display any relevant alerts or dialogs
Wire up external event handlers
Undo operations in OnPause
OnPause
◦
◦
◦
◦
◦
Commit changes to persistent data
Destroy or clean up other objects consuming resources
Ramp down frame rates and pausing animations
Unregister external event handlers or notification handlers
Clear dialogs and alerts
15. Activity Methods
OnStop - activity is no longer visible to the
user
◦ A new activity is being started and is covering
up this activity.
◦ An existing activity is being brought to the
foreground.
◦ The activity is being destroyed.
OnDestroy - final method
◦ Kill threads
OnRestart – prior to a restart
◦ Then OnStart – most reinit logic goes there
16. Managing State
Bundle
◦ Primitive data types
Custom Class
◦ Complex data (ex. Bitmap)
Roll Your Own
◦ Circumvent the configuration change
lifecycle
19. iOS Lifecycle Methods
OnActivated
◦ upon launch or when app returns to foreground
OnResignActivation
◦ upon an interruption such as a text or phone call
DidEnterBackground
◦ user accepts the phone call
◦ save user data and tasks
◦ remove sensitive information from the screen
WillEnterForeground
◦ App returns to foreground from background or suspension
◦ Restore state saved during DidEnterBackground
◦ Then call OnActivated
WillTerminate
◦ App shuts down and process is destroyed
◦ If multitasking is not available on the device or the OS
version
◦ Memory is low
◦ User manually terminates a backgrounded application.
21. iOS:Events, Protocols, and
Delegates
Events
◦ Xamarin.iOS exposes .NET events on
UIKit controls
Protocols
◦ Like a C# interface with optional methods
◦ Xamarin.iOS implements using abstract classes
Delegates
◦ strongly and weakly typed
◦ not be confused with C# delegates
22. Performance
Mobile users expect it
Delays are annoying and alienate
users
Android Activities and iOS Delegates?
◦ run in the foreground UI Thread
Long-running code should go into a…
24. Good Candidates for a
Background Thread
Operations that
◦ Make requests off of the device
Retrieve data (web service, RSS feed)
Connect to network
Downloads
◦ Run more than 50 milliseconds
◦ May block the user interface thread
Watchdog thread to kill the app
25. Async/Await
async
◦ method declaration
◦ lambda
◦ anonymous method
await
◦ code stops and waits
◦ spawns background thread
◦ UI thread is not blocked
◦ resumes at the same point in the code
32. MVC
Model
◦ Can use them
Views
◦ Must use them
Controllers
◦ Android Activities
◦ iOS AppDelegates
33. Cross-Platform Mobile
Development In C#
Dan Hermes
Mobile Consultant
Lexicon Systems
Are You Mobilizing?
Website: www.lexiconsystemsinc.com
Email: dan@lexiconsystemsinc.com
Phone: 781-526-0738
Twitter: @lexiconsystems
Blog: www.itshopkeeping.com
Notas do Editor
What about App select button?
Partial or transparent Activity? HOW TO PAUSE????
Restart should go to OnCreate()?? (so bundle can reload)
Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).
Restart should go to OnCreate()?? (so bundle can reload)
http://docs.xamarin.com/guides/cross-platform/application_fundamentals/backgrounding/part_1_introduction_to_backgrounding_in_ios/Not Running - The application has not yet been launched on the device.Running/Active - The application is on the screen, and is executing code in the foreground.Inactive - The application is interrupted by an incoming phone call, text, or other interruption.Backgrounded - The application moves into the background and continues executing background code.Suspended - If the application does not have any code to run in the background, or if all code has completed, the app will be Suspended by the OS. A suspended application's process is kept alive, but the application is unable to execute any code in this state.Return to Not Running/Termination (Rare) - Occasionally, the application's process is destroyed, and the application returns to the Not Running state. This happens in low-memory situations, or if the user manually terminates the application.
OnActivated - This is called the first time the application is launched, and every time the app comes back into the foreground. This is the place to put code that needs to run every time the app is opened.OnResignActivation - If the user receives an interruption such as a text or phone call, this method gets called and the app is temporarily inactivated. Should the user accept the phone call, the app will be sent to the background.DidEnterBackground - Called when the app enters the backgrounded state, this method gives an application about five seconds to prepare for possible termination. Use this time to save user data and tasks, and remove sensitive information from the screen.WillEnterForeground - When a user returns to a backgrounded or suspended application, and launches it into the foreground, WillEnterForeground gets called. This is the time to prepare the app to take the foreground by rehydrating any state saved during DidEnterBackground. OnActivated will be called immediately after this method completes.WillTerminate - The application is shut down, and its process is destroyed. This method only gets called if multitasking is not available on the device or the OS version, if memory is low, or if the user manually terminates a backgrounded application. Note that suspended applications that get terminated will not callWillTerminate.
OnActivated - This is called the first time the application is launched, and every time the app comes back into the foreground. This is the place to put code that needs to run every time the app is opened.OnResignActivation - If the user receives an interruption such as a text or phone call, this method gets called and the app is temporarily inactivated. Should the user accept the phone call, the app will be sent to the background.DidEnterBackground - Called when the app enters the backgrounded state, this method gives an application about five seconds to prepare for possible termination. Use this time to save user data and tasks, and remove sensitive information from the screen.WillEnterForeground - When a user returns to a backgrounded or suspended application, and launches it into the foreground, WillEnterForeground gets called. This is the time to prepare the app to take the foreground by rehydrating any state saved during DidEnterBackground. OnActivated will be called immediately after this method completes.WillTerminate - The application is shut down, and its process is destroyed. This method only gets called if multitasking is not available on the device or the OS version, if memory is low, or if the user manually terminates a backgrounded application. Note that suspended applications that get terminated will not callWillTerminate.
http://docs.xamarin.com/guides/cross-platform/application_fundamentals/backgrounding/part_1_introduction_to_backgrounding_in_ios/Not Running - The application has not yet been launched on the device.Running/Active - The application is on the screen, and is executing code in the foreground.Inactive - The application is interrupted by an incoming phone call, text, or other interruption.Backgrounded - The application moves into the background and continues executing background code.Suspended - If the application does not have any code to run in the background, or if all code has completed, the app will be Suspended by the OS. A suspended application's process is kept alive, but the application is unable to execute any code in this state.Return to Not Running/Termination (Rare) - Occasionally, the application's process is destroyed, and the application returns to the Not Running state. This happens in low-memory situations, or if the user manually terminates the application.
Android Activities and iOS Delegates?run in the foreground UI ThreadMobile devices refresh their screen at 60 frames per second. A screen refresh will take place every 16.7 milliseconds. Movies are typically shown at a rate of 24 or 25 frames per second. This data indicates that the human eye can detect changes starting at around 20 frames per second. Anything that delays an update can cause a delay that is perceptible to the human eyea watchdog built into a device will kill the application that is blocking the UI thread.
Uses the Task Parallel Library(TPL) ???Need code exampleThe recommended approach?await operator can be applied to a Task inside a method marked as async. It causes the method to stop execution at that point and wait until the task completes.
Uses the Task Parallel Library(TPL) ???Need code exampleThe recommended approach?await operator can be applied to a Task inside a method marked as async. It causes the method to stop execution at that point and wait until the task completes.
One of the new features of .NET 4.5/Mono 3.2 is the inclusion of the System.Net.Http.HttpClient class. This class is designed to perform asynchronous requests via the GET, POST, PUT, & DELETE Http verbs. The method calls for this are: There are other methods that this class exposes. Some of these methods are: CancelPendingRequests GetByteArrayAsync GetStreamAsync SendAsync