Profile

Cover photo
Jared Kells
147 followers|19,575 views
AboutPostsPhotosVideosReviews

Stream

Jared Kells

Shared publicly  - 
 
Hi Dave
1
Steve Parkin's profile photo
Add a comment...

Jared Kells

Shared publicly  - 
1
Add a comment...

Jared Kells

Shared publicly  - 
 
Global state in Android apps

I keep reading advice online about storing state in static singleton objects or in the Application object. It's just bad advice and is going to lead to unpredictable behaviour.

The Application object and any static variables in your application exist for the lifetime of your hosting process. The lifetime of your hosting process is different to the lifetime of your Activity

When one of your activities is launched, the system checks if a process is already running for your application. If it is then that process is used to launch the new activity. If not then a new process is started and a new Application object is created.

When you navigate through activities inside your application the current process is always reused so your static variables continue to exist and you always have the same instance of the Application object. Sometime after all your activities have ended the hosting process calls onDestroy on the Application object and the process is terminated.

Most of the time the lifecycle looks like this
 * Process Created
 * Application Created
 * Activity 1
 * Activity 2
 * Activity 3
 * Application Destroyed
 * Process terminated
 
 It seems like the application object would be a good place to store state but the only guarantee is that an Application object will be created before any of your activities run and will be destroyed after all your activities are destroyed. It's never guaranteed that it will be the same object.
 
 What happens rarely in development but regularly in the real world is the following
 
 * Process Created
 * Application Created
 * Activity 1
 * Activity 2
 * Phone call received or different app launched. ( Application backgrounded )
 * Some arbitrary time later... 
 * Application object destroyed
 * Process terminated
 .....
 * User re-enters the application
 * Process Created
 * Application Created
 * Activity 2 resumed
 
All your static variables are now null and you have a brand new Application object
 
 I think this is the cause of those Android apps that always fail to resume after an extended delay. They either crash or flash a screen for half a second and then finish.

Some links to accepted bad advice:

http://stackoverflow.com/questions/13633092/efficiently-passing-custom-object-data-between-android-activities-mono-android

http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables
1
Add a comment...

Jared Kells

Shared publicly  - 
 
* Can you do any formatting in a G+ post? *
<strong>test</strong>
</hr>
-----
* item 1
* item 2

Heading
------------

Heading
=======

<h1>Heading</h1>
1
Gerald Kaszuba's profile photoJared Kells's profile photo
2 comments
 
thanks
Add a comment...

Jared Kells

Shared publicly  - 
1
John Jeffery's profile photoJared Kells's profile photo
2 comments
 
nup, not yet
Add a comment...
Have him in circles
147 people
Tony Young's profile photo
Albert Tsai's profile photo
Jason Persson's profile photo
Jahmai Lay's profile photo
Jessica Hull's profile photo
Brennan Hatton's profile photo
Piper Wahlquist's profile photo

Jared Kells

Shared publicly  - 
 
Updating 20 to 30 nuget packages is a bit scary. Hope my tests work....
1
David GAZE's profile photoJared Kells's profile photo
3 comments
 
Lol. Cheeky little miss. 
Add a comment...

Jared Kells

Shared publicly  - 
 
I was confused by .prototype and .__proto__ in Javascript. This post help clear it up for me: http://sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/
1
Add a comment...

Jared Kells

Shared publicly  - 
 
Heading

this should be in italics?

```All your base are belong to us.
1
Add a comment...

Jared Kells

Shared publicly  - 
 
Captured my first portal #ingress
3
Add a comment...

Jared Kells

Shared publicly  - 
 
Dear +Google Developers. Please call Android 6 "Lamington" http://en.wikipedia.org/wiki/Lamington 

Kind Regards,
Australia
3
Anthony Carrick's profile photoDonna Cluny Gardner's profile photoAdrian De Santi's profile photoJared Kells's profile photo
8 comments
 
Let's try to make this take off. 

Posted in /r/Australia http://redd.it/19rmew
and in /r/Android http://redd.it/19rnmw
Add a comment...
People
Have him in circles
147 people
Tony Young's profile photo
Albert Tsai's profile photo
Jason Persson's profile photo
Jahmai Lay's profile photo
Jessica Hull's profile photo
Brennan Hatton's profile photo
Piper Wahlquist's profile photo
Basic Information
Gender
Male
A+++ Nice clean rooms, great atmosphere, great value. We had a really nice time staying at the Aalberg. The staff were friendly and it had a great atmosphere. We will be staying here again next year. The special German apple pie was delicious!
Quality: ExcellentFacilities: ExcellentService: Excellent
Public - 2 years ago
reviewed 2 years ago
1 review
Map
Map
Map