advanced android app lifecycle + patterns
Post on 27-Nov-2014
788 Views
Preview:
DESCRIPTION
TRANSCRIPT
App Lifecycle + Advanced Architectures/Patterns/Problems
Android
bryan costanichem: bryanc@xamarin.comtw: @bryancostanichslides: slideshare.net/bryancostanichcode: 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 LifecycleImportant Methods
OnCreate ( ) - Initialize StufOnResume ( ) - Begin LifeOnPause ( ) - Stop/Pause
Code Walkthrough 1Activity 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 SingletonApp Singleton Class
bool IsInitialized PropertyInitialized Event
ActivityBaseCheck to see if
App.Current.IsInitialized
App
public static App() protected - ctor ()public static App Currentpublic bool IsInitialized
public event Initialized
Code Walkthrough 2App 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 won’t 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 3App 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
Code Walkthrough 4
protected void HandleUnhandledException (object sender, UnhandledExceptionEventArgs args){ Exception e = (Exception) args.ExceptionObject; Console.WriteLine ("Unhandled Exception = " + e.Message);}
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 5Asynchronous Initialization
Q+A
top related