Nov 10 | 3:55 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Ratul S. | has entered the room |
Mark M. |
hello, Rahul!
|
Mark M. |
how can I help you today?
|
Ratul S. |
hello, I'm a new subscriber and new to Android world. Just wanted to check in with an office hour to see what it is like.
|
Mark M. |
it is really for if you have questions to ask
|
Mark M. |
the number of attendees varies, usually only a couple of people
|
Mark M. |
I kinda rotate between them to handle their questions
|
Ratul S. |
I'm sorry, I didn't know that.
|
Nov 10 | 4:00 PM |
Aaron M. | has entered the room |
Mark M. |
Ratul: no problem
|
Aaron M. | has left the room |
Aaron M. | has entered the room |
Mark M. |
hello, Aaron!
|
Aaron M. |
hello
|
Ratul S. |
I will come back when I have questions. Thanks!
|
Mark M. |
Aaron: your turn! do you have a question?
|
Mark M. |
Ratul: OK, sounds good
|
Aaron M. |
Yes i have a question about activity exceptions
|
Aaron M. |
Can i give an example?
|
Mark M. |
sure
|
Ratul S. | has left the room |
Aaron M. |
I created a demo app that i used to find out what the OS does with an activity when it crashes. https://gist.github.com/px-amaac/cbb318339ccbee...
|
Mark M. |
um, OK
|
Aaron M. |
basically i was unaware that the OS tries to restart whatever activity is on the backstack when it crashes
|
Aaron M. |
My question "is there a way to prevent this?"
|
Mark M. |
prevent what?
|
Mark M. |
exceptions?
|
Aaron M. |
Basically i have an app and when it crashes it will crash multiple times
|
Mark M. |
that doesn't really help me much
|
Nov 10 | 4:05 PM |
Aaron M. |
sorry i know the reason for the multiple crashes and i am fixing the core reason for multiple crashes but
|
Aaron M. |
i always thought that when the app crashed that was it
|
Mark M. |
control returns to whatever preceded the activity
|
Mark M. |
that could be one of your activities
|
Mark M. |
or it could be something else, depending on what is on the back stack
|
Aaron M. |
so basically the app should resume gracefully if it crashes?
|
Aaron M. |
each activity rather
|
Mark M. |
well, the activity will resume in lots of circumstances, such as the user pressing BACK
|
Mark M. |
(from some subsequent activity)
|
Mark M. |
and so it should be resuming gracefully in general
|
Aaron M. |
yes in my problem there is some settings that get destroyed in the crash
|
Mark M. |
what are "settings" and how are they "destroyed"?
|
Aaron M. |
so when the activity comes back the api calls fail due to lack of these settings
|
Aaron M. |
and i am not sure how they are destroyed
|
Mark M. |
what are "settings"?
|
Aaron M. |
basically i have an API class that has environment settings in it.
|
Aaron M. |
its a singleton i believe
|
Mark M. |
then you have *much* bigger problems
|
Aaron M. |
ya i know
|
Mark M. |
suppose we have Activity A
|
Nov 10 | 4:10 PM |
Mark M. |
Activity A depends upon this singleton
|
objectref | has entered the room |
Aaron M. |
yup
|
Mark M. |
and Activity A does no defensive programming, lazy-initialization, or the like
|
Mark M. |
(objectef: hello! I will be with you shortly!)
|
Mark M. |
Aaron: but, in the normal case, when Activity A starts up, it is fine
|
objectref |
Hello!
|
Mark M. |
so Activity A is on the screen
|
Mark M. |
and the user presses HOME
|
Mark M. |
time passes
|
Mark M. |
your process is terminated due to low memory conditions
|
Mark M. |
but it hasn't been *so* long that your task has rolled off the recent-tasks list
|
Mark M. |
now the user returns to your app, such as via the recent-tasks list
|
Mark M. |
Android will create a new process for you
|
Mark M. |
followed by a new instance of Activity A
|
Mark M. |
and will put A in the foreground
|
Mark M. |
and, if you are not doing anything to handle this case, A will blow up
|
Aaron M. |
without any of the init issues
|
Mark M. |
because your singleton may not be set up
|
Aaron M. |
matheds running
|
Aaron M. |
ok and does this new process thing happen when the app crashes also?
|
Mark M. |
I didn't think so, but I can't rule it out -- it's been a while since I played with this scenario
|
Mark M. |
but, more importantly, even *without* the crash, you have to deal with this case
|
Aaron M. |
because i notice that i lose my logcat and debug stuff on crash
|
Aaron M. |
yes i agree
|
Mark M. |
any singleton has to be a cache, and code dependent upon the singleton has to be able to survive if the cache is empty
|
Mark M. |
fix that, and your problems with the activity getting control after a subsequent activity crashes should go away
|
Mark M. |
let me take a question from objectref, and I will be back with you shortly
|
Aaron M. |
Do you mind if a stick around i have a followup question but i want objectref to get his question in.
|
Mark M. |
objectref: your turn! do you have a question?
|
Aaron M. |
cool
|
Nov 10 | 4:15 PM |
Mark M. |
objectref: your turn! do you have a question?
|
objectref |
Hi
|
objectref |
No, actually i got my subscription yesterdays and i started reading the book, so for the moment i do not have one. I just got in to say thanks for the nice book!
|
Mark M. |
you are very welcome
|
Mark M. |
if you think of a question, let me know
|
Mark M. |
Aaron: back to you! do you have another question?
|
Aaron M. |
Ya seriously I recommended it to an android class that i TA for. They loved it.
|
Mark M. |
FWIW, the book is free for qualifying college and university courses: http://commonsware.com/blog/2010/10/01/free-and...
|
Aaron M. |
I will let the teacher know
|
Aaron M. |
It has been suggested to me that i put the initialization of singletons in an extended application class to ensure that they will always be there. Any thoughts one way or another on this?
|
Nov 10 | 4:20 PM |
Mark M. |
it usually doesn't solve any problems
|
Mark M. |
there is little that you can do in an Application subclass that you cannot do in a regular singleton
|
Aaron M. |
But application is always there. So the problem of activity A starting in a new process. Would this not fix that because the new process would spawn the application class then the activity?
|
Mark M. |
oh, the Application class will be spawned
|
Mark M. |
so would a regular singleton, assuming that you are using some sort of static getInstance() accessor
|
Mark M. |
that does lazy-initialization
|
Aaron M. |
Ok. that makes sense
|
Aaron M. |
since there is nobody else another one?
|
Mark M. |
go right ahead!
|
Nov 10 | 4:25 PM |
Aaron M. |
In all of this i have a singleton class that handles my oauth tokens and has an async task to get tokens. In the pattern we just discussed of using a getinstance method how do i make that call with the async task and then prevent the app from continuing until it has tokens? basically it should be a synchronous call but because we want it off the UI thread we make it asynchronous.
|
Aaron M. |
callback?
|
Mark M. |
your activity should basically have two states: ready and not-ready
|
Mark M. |
if, when it is created, the desired token(s) do not exist, it goes into not-ready mode, then forks a thread/AsyncTask/whatever to do the background work
|
Mark M. |
it then moves into ready mode when that work is done (onPostExecute(), event bus event received, etc.)
|
Mark M. |
if, however, the desired token(s) exist already at creation time, the activity can just roll into ready mode
|
Nov 10 | 4:30 PM |
Aaron M. |
ok.
|
Aaron M. |
Thankyou very much
|
Mark M. |
sure
|
Aaron M. |
Ill see you again some time you were a great help
|
Mark M. |
happy to be useful!
|
Aaron M. |
Is there a way to get a transcript of this?
|
Mark M. |
one will be posted to http://commonsware.com/office-hours/ shortly after the chat ends
|
Aaron M. |
thankyou
|
Nov 10 | 4:35 PM |
Mark M. |
if anyone has any questions, go right ahead
|
Nov 10 | 4:50 PM |
objectref | has left the room |
Nov 10 | 4:55 PM |
Aaron M. |
Have a good day. Thankyou for the help.
|
Mark M. |
you are very welcome
|
Aaron M. | has left the room |
Nov 10 | 5:00 PM |
Mark M. | turned off guest access |