Advanced Android App Life cycle

Embed Size (px)

Citation preview

  • 8/12/2019 Advanced Android App Life cycle

    1/15

    App Lifecycle +

    Advanced Architectures/Patterns/ProblemsAndroid

  • 8/12/2019 Advanced Android App Life cycle

    2/15

    bryan costanichem: [email protected]: @bryancostanich

    slides: slideshare.net/bryancostanich

    code: bit.ly/1avqivC

    http://slideshare.net/bryancostanichhttp://bit.ly/1avqivChttp://bit.ly/1avqivChttp://slideshare.net/bryancostanich
  • 8/12/2019 Advanced Android App Life cycle

    3/15

    What We'll Cover

    Review Activity Lifecycle Relation to the App

    Lifecycle

    Review States + Important

    Events Background Updates

    Configuration Changes

    App Lifecycle + PatHandling

    App Initialization

    Application Crash

    Restarts Asynchronous Init

  • 8/12/2019 Advanced Android App Life cycle

    4/15

    Activity LifecycleImportant Methods

    OnCreate ( ) - Initialize StufOnResume ( ) - Begin Life

    OnPause ( ) - Stop/Pause

  • 8/12/2019 Advanced Android App Life cycle

    5/15

    Code Walkthrough 1Activity Lifecycle

  • 8/12/2019 Advanced Android App Life cycle

    6/15

    App InitializationCommon to need to initialize things at the beginning of the App Li

    Sometimes these can take a whileCommon approach is to show a loading screen

  • 8/12/2019 Advanced Android App Life cycle

    7/15

    Pattern: AppSingletonApp Singleton Class

    bool IsInitialized PropertyInitialized Event

    ActivityBase

    Check to see if App.Current.IsInitialized

    Ap

    public static

    App()protected - c

    ()public static A

    Currentpublic bool

    IsInitializedpublic event

    Initialized

  • 8/12/2019 Advanced Android App Life cycle

    8/15

    Code Walkthrough 2App Initialization

  • 8/12/2019 Advanced Android App Life cycle

    9/15

    App Initialization After Cras

    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 nav

    history - Painful

    Need to enable initialization on all activities

    Sometimes want to force load of launch activity

  • 8/12/2019 Advanced Android App Life cycle

    10/15

    Code Walkthrough 3App Initialization, Post-Crash

  • 8/12/2019 Advanced Android App Life cycle

    11/15

    Unhandled Exceptions

    In other app platforms, can be handled at an App leve

    AppDomain.CurrentDomain.UnhandledException +=

    HandleUnhandledException;

    Trickier in Android - Will cause a Spectacular Crash

    Some day, this story will be better, can at least log no

    See in Android Log

  • 8/12/2019 Advanced Android App Life cycle

    12/15

    Code Walkthrough 4

    protected void HandleUnhandledException (objectsender,

    UnhandledExceptionEventArgsargs){

    Exceptione = (Exception) args.ExceptionObject;

    Console.WriteLine ("Unhandled Exception = " + e.Message);

    }

  • 8/12/2019 Advanced Android App Life cycle

    13/15

    Waiting for Async Initializat

    Sometimes app needs to wait for multiple asynchrono

    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

  • 8/12/2019 Advanced Android App Life cycle

    14/15

    Code Walkthrough 5Asynchronous Initialization

  • 8/12/2019 Advanced Android App Life cycle

    15/15