Вы находитесь на странице: 1из 15

Android

App Lifecycle + Advanced Architectures/Patterns/Problems

bryan costanich
em: bryanc@xamarin.com tw: @bryancostanich slides: slideshare.net/bryancostanich code: bit.ly/1avqivC

What We'll Cover


Review Activity Lifecycle

Relation to the App Lifecycle


Review States + Important Events Background Updates Configuration Changes

App Lifecycle + Patterns for Handling App Initialization Application Crashes and Restarts Asynchronous Initialization

Activity Lifecycle
Important Methods OnCreate ( ) - Initialize Stuf

OnResume ( ) - Begin Life


OnPause ( ) - Stop/Pause

Code Walkthrough 1
Activity Lifecycle

App Initialization
Common to need to initialize things at the beginning of the App Lifecycle Sometimes these can take a while

Common approach is to show a loading screen

Pattern: App Singleton


App Singleton Class bool IsInitialized Property

Initialized Event
ActivityBase Check to see if App.Current.IsInitialized

Ap p public static App() protected - ctor () public static App Current public bool IsInitialized public event Initialized

Code Walkthrough 2
App Initialization

App Initialization After Crash


Unhandled Exceptions will cause Android to kill the process AppDomain and all objects in it will be cleaned up by Mono Android will still attempt to launch last running activity If it relies on initialization, activity wont make sense

If activity re-launch fails, it moves backwards through the navigation history - Painful
Need to enable initialization on all activities

Sometimes want to force load of launch activity

Code Walkthrough 3
App Initialization, Post-Crash

Unhandled Exceptions
In other app platforms, can be handled at an App level AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException; Trickier in Android - Will cause a Spectacular Crash Some day, this story will be better, can at least log now See in Android Log

protected void HandleUnhandledException (object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; Console.WriteLine ("Unhandled Exception = " + e.Message); }

Code Walkthrough 4

Waiting for Async Initializations


Sometimes app needs to wait for multiple asynchronous inits
e.g.: binding to more than one service or service + web request etc. Same pattern as before, but keep track of inits When all inits complete, raise Initialized event

Code Walkthrough 5
Asynchronous Initialization

Вам также может понравиться