Office Hours — Today, November 10

Thursday, November 6

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!
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
the number of attendees varies, usually only a couple of people
I kinda rotate between them to handle their questions
Ratul S.
I'm sorry, I didn't know that.
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?
Ratul: OK, sounds good
Aaron M.
Yes i have a question about activity exceptions
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
My question "is there a way to prevent this?"
Mark M.
prevent what?
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
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
i always thought that when the app crashed that was it
Mark M.
control returns to whatever preceded the activity
that could be one of your activities
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?
each activity rather
Mark M.
well, the activity will resume in lots of circumstances, such as the user pressing BACK
(from some subsequent activity)
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
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.
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
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
(objectef: hello! I will be with you shortly!)
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
and the user presses HOME
time passes
your process is terminated due to low memory conditions
but it hasn't been *so* long that your task has rolled off the recent-tasks list
now the user returns to your app, such as via the recent-tasks list
Android will create a new process for you
followed by a new instance of Activity A
and will put A in the foreground
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
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
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
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
fix that, and your problems with the activity getting control after a subsequent activity crashes should go away
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
4:15 PM
Mark M.
objectref: your turn! do you have a question?
objectref
Hi
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
if you think of a question, let me know
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
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?
4:20 PM
Mark M.
it usually doesn't solve any problems
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
so would a regular singleton, assuming that you are using some sort of static getInstance() accessor
that does lazy-initialization
Aaron M.
Ok. that makes sense
since there is nobody else another one?
Mark M.
go right ahead!
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.
callback?
Mark M.
your activity should basically have two states: ready and not-ready
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
it then moves into ready mode when that work is done (onPostExecute(), event bus event received, etc.)
if, however, the desired token(s) exist already at creation time, the activity can just roll into ready mode
4:30 PM
Aaron M.
ok.
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
4:35 PM
Mark M.
if anyone has any questions, go right ahead
4:50 PM
objectref
has left the room
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
5:00 PM
Mark M.
turned off guest access

Thursday, November 6

 

Office Hours

People in this transcript

  • Aaron McI
  • Mark Murphy
  • objectref
  • Ratul Sarna