SlideShare a Scribd company logo
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

• App Lifecycle + Patterns
for Handling
• App Initialization

• Review States +
Important Events

• Application Crashes and
Restarts

• Background Updates

• Asynchronous
Initialization

• Configuration Changes
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

App
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 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 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
Q+A

More Related Content

What's hot (12)

PPTX
Ug. marketplace testing
Транслируем.бел
 
PDF
Understanding meteor
M A Hossain Tonu
 
PDF
How Testing Helped The API 0.6 Migration
Shaun McDonald
 
PDF
Activity
Michael Pan
 
PDF
The what, why and how of web analytics testing
Anand Bagmar
 
PDF
A Beginners Guide to Erlang_Erlang Factory Lite_Munich 2013
Wooga
 
PPTX
TiConnect: Memory Management in Titanium apps
Tim Poulsen
 
KEY
Titanium appcelerator best practices
Alessio Ricco
 
PDF
WordCamp Bucharest 2017
Alexandra Anghel
 
PDF
Unit Testing Lightning Components with Jasmine
Keir Bowden
 
PDF
Using JHipster for generating Angular/Spring Boot apps
Yakov Fain
 
PDF
Challenges in writing roboelectric tests
BlrDroid
 
Ug. marketplace testing
Транслируем.бел
 
Understanding meteor
M A Hossain Tonu
 
How Testing Helped The API 0.6 Migration
Shaun McDonald
 
Activity
Michael Pan
 
The what, why and how of web analytics testing
Anand Bagmar
 
A Beginners Guide to Erlang_Erlang Factory Lite_Munich 2013
Wooga
 
TiConnect: Memory Management in Titanium apps
Tim Poulsen
 
Titanium appcelerator best practices
Alessio Ricco
 
WordCamp Bucharest 2017
Alexandra Anghel
 
Unit Testing Lightning Components with Jasmine
Keir Bowden
 
Using JHipster for generating Angular/Spring Boot apps
Yakov Fain
 
Challenges in writing roboelectric tests
BlrDroid
 

Similar to Advanced android app lifecycle + Patterns (20)

DOCX
Activity
roopa_slide
 
DOCX
Activity
NikithaNag
 
DOCX
Activity
NikithaNag
 
DOCX
Activity
roopa_slide
 
ODP
Anatomy of android application
Nikunj Dhameliya
 
PDF
Android development Training Programme Day 2
DHIRAJ PRAVIN
 
PPTX
Lecture #4 activities & fragments
Vitali Pekelis
 
PDF
Mad textbook 63-116
PrathishGM
 
PDF
Lecture 3 getting active through activities
Ahsanul Karim
 
PPT
Android lifecycle
Kumar
 
PPTX
1a.Activity life cycle basics of android.pptx
BhawnaSaini45
 
PPT
Android
Natasha Ramírez
 
PDF
Crash wars - The handling awakens
Željko Plesac
 
PPTX
Unit 5 Activity and Activity Life Cycle.pptx
ShantanuDharekar
 
PDF
Android activities & views
ma-polimi
 
PDF
Android activity
Krazy Koder
 
PDF
Android activity
Krazy Koder
 
PDF
Bcsf13a019_mcqs_ead
MarYam IqBal
 
PPTX
Dori waldman android _course
Dori Waldman
 
PDF
02 programmation mobile - android - (activity, view, fragment)
TECOS
 
Activity
roopa_slide
 
Activity
NikithaNag
 
Activity
NikithaNag
 
Activity
roopa_slide
 
Anatomy of android application
Nikunj Dhameliya
 
Android development Training Programme Day 2
DHIRAJ PRAVIN
 
Lecture #4 activities & fragments
Vitali Pekelis
 
Mad textbook 63-116
PrathishGM
 
Lecture 3 getting active through activities
Ahsanul Karim
 
Android lifecycle
Kumar
 
1a.Activity life cycle basics of android.pptx
BhawnaSaini45
 
Crash wars - The handling awakens
Željko Plesac
 
Unit 5 Activity and Activity Life Cycle.pptx
ShantanuDharekar
 
Android activities & views
ma-polimi
 
Android activity
Krazy Koder
 
Android activity
Krazy Koder
 
Bcsf13a019_mcqs_ead
MarYam IqBal
 
Dori waldman android _course
Dori Waldman
 
02 programmation mobile - android - (activity, view, fragment)
TECOS
 
Ad

More from bryan costanich (9)

PDF
Hardware hackers - hacking appliances with netduino + xamarin
bryan costanich
 
PDF
Teardown Conference: hacking appliances with netduino + xamarin
bryan costanich
 
PPTX
Hacking your coffee maker; building a connected appliance with Netduino and X...
bryan costanich
 
PPTX
Futures in Computing
bryan costanich
 
PPT
C# rocks
bryan costanich
 
PPT
Cross Platform Mobile Development with Xamarin
bryan costanich
 
KEY
Going mobile - A Technical Job Prep for Vassar Students
bryan costanich
 
PPT
Cross-Platform Mobile Development in Visual Studio
bryan costanich
 
KEY
Cross Platform Development with Xamarin
bryan costanich
 
Hardware hackers - hacking appliances with netduino + xamarin
bryan costanich
 
Teardown Conference: hacking appliances with netduino + xamarin
bryan costanich
 
Hacking your coffee maker; building a connected appliance with Netduino and X...
bryan costanich
 
Futures in Computing
bryan costanich
 
C# rocks
bryan costanich
 
Cross Platform Mobile Development with Xamarin
bryan costanich
 
Going mobile - A Technical Job Prep for Vassar Students
bryan costanich
 
Cross-Platform Mobile Development in Visual Studio
bryan costanich
 
Cross Platform Development with Xamarin
bryan costanich
 
Ad

Recently uploaded (20)

PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 

Advanced android app lifecycle + Patterns

  • 2. bryan costanich em: [email protected] tw: @bryancostanich slides: slideshare.net/bryancostanich code: bit.ly/1avqivC
  • 3. What We'll Cover • Review Activity Lifecycle • Relation to the App Lifecycle • App Lifecycle + Patterns for Handling • App Initialization • Review States + Important Events • Application Crashes and Restarts • Background Updates • Asynchronous Initialization • Configuration Changes
  • 4. Activity Lifecycle Important Methods OnCreate ( ) - Initialize Stuf OnResume ( ) - Begin Life OnPause ( ) - Stop/Pause
  • 6. 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
  • 7. Pattern: App Singleton App Singleton Class bool IsInitialized Property Initialized Event ActivityBase Check to see if App.Current.IsInitialized App public static App() protected - ctor () public static App Current public bool IsInitialized public event Initialized
  • 8. Code Walkthrough 2 App Initialization
  • 9. 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
  • 10. Code Walkthrough 3 App Initialization, Post-Crash
  • 11. 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
  • 12. protected void HandleUnhandledException (object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; Console.WriteLine ("Unhandled Exception = " + e.Message); } Code Walkthrough 4
  • 13. 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
  • 15. Q+A

Editor's Notes

  • #4: C# or Java. Same problems.
  • #5: the most important methods to override and implement are OnCreate(), OnResume(), and OnPause(). > OnCreate() - Load/inflate your UI, instantiate any controls, get references to controls in your view, wire up any local event handlers > OnResume() - Rehydrate UI state, Wire up any external event/notification handlers, start updating UI, basically, undo ANYTHING that you do in OnPause() > OnPause() - Remove any external event handlers, dismiss any dialogs, pause UI updates. Why? because if the Activity is paused and then reactivated, OnResume will be the only next method. OnCreate will be called when the Activity is first Instanced, but OnResume is the only method guaranteed to be called when an activity is activated.
  • #6: Cover: > app.cs - we’re using this to provide an external update to subscribe to MainActivity.cs > OnCreate > OnResume > OnPause > AddHandlers > RemoveHandlers
  • #9: Cover: App.cs > Initialized Event > IsInitialized property > .ctor: now takes a little while, also that it’s in a Task() because it is blocking (started from a UI thread)
  • #13: Examine: App.cs > wire up the AppDomain.CurrentDomain.UnhandledException handler
  • #14: If you have a single asynchronous initialization, it’s easy to handle. If you’re using the pattern we’ve been examining, you simply raise the App.Initialized event in the completion handler. However, sometimes you need to initialize multiple asynchronous things. For example, if you’re binding to more than one service, or you’re binding to a service and also waiting for a web request to finish, etc. Handling this scenario is actually quite simple. We use the same pattern as before, but we extend it but keeping track of the number things that require initialization, and when we hit the threshold, that is, when all the initializations complete, we raise the Initialized event and we’re on our way.
  • #15: Examine: App.cs > totalInitCount > currentInitCount > IncrementInitCount > .ctor - service connected calls IncrementInitCount Also Service Binding Generics