Office Hours — Today, March 9

Thursday, March 4

Mar 9
8:20 PM
Mark M.
has entered the room
Mark M.
turned on guest access
8:45 PM
Sebastian
has entered the room
Mark M.
howdy!
Sebastian
hi there
Gene R.
has entered the room
Gene R.
Hello
Mark M.
howdy, Gene!
Gene R.
I have a question regarding Android Preferences.
Mark M.
go ahead
Gene R.
I'm hoping you can help me answer.
Mark M.
go ahead
Gene R.
I am writing an App that is using Preferences. I have the preference screen set up. On the onResume() function, I set my preferences based upon what the user selects on the preference screen.
Mark M.
ok
Gene R.
However, when the user first enters the Preferences screen, the defaults preferences (as defined in the xml) always appear.
Do I need to add some code to get the preference screen to display the current application settings/preferences?
8:50 PM
Mark M.
not usually
those are the preference example from _The Busy Coder's Guide to Android Development_
they too use the onResume() approach
they do not exhibit this problem, AFAIK
you might compare what you have to those projects and see where things differ
Gene R.
Okay. I'll do that. But just to clarify. In the onResume() function of the main activity, I am reading the preferences and using those to set various states in my App. But upon startup, could there be a mismatch of what the preference screen is displaying and what is actually set in my App?
Mark M.
depends on how you are reading the preferences
PreferenceManager.getDefaultSharedPreferences()
that is what you should be using
8:55 PM
Gene R.
I am indeed using getDefaultSharedPreferences(). I will look over the preference example more closely to see why I am getting a mismatch. Would there normally be any code in the instantiation of the Preference class?
other than the call to addPreferenceFromResource(...)
Mark M.
you don't instantiate the Preference class
oh, you mean PreferenceActivity
no, that's all that is needed
Gene R.
I'm sorry, I mean in the definition of the PreferenceActivity class.
Great! Thank You, appreciate the help! By the way I really enjoy your books. I am just getting started in Android Development, and your books have been an invaluable resource. I almost bought the print version, then found out about Commonsware.
Mark M.
anyone have another question?
thanks!
the Warescription is definitely the better deal, if you don't need it printed
or are willing to print it yourself
Gene R.
I have a question regarding AudioRecord..
Sebastian
i have one. and also i think commonsware is great. i just subcribed last week.
Mark M.
Gene: Hmmmmm...I haven't used AudioRecord yet, so my knowledge is limited
Sebastian: thanks!
9:00 PM
Sebastian
there are two http stacks(java.net and the apache one) in android is there anyone more suitable then the other?
Mark M.
I tend to use HttpClient unless I'm reusing code that used java.net.*
HttpClient is a bit more full-featured
Sebastian
are there benefits/or is it good practice to use a contentprovider within my app, even if i don't plan to make the provider available to an outside application?
Mark M.
not really
Gene R.
Quick Question: If I am iterating through a for loop, is it okay to use Array.length as the condition of the for loop, or is it recommended in Android to copy the length to a local variable and use that as the condition? (ie. for (int i=0;i<myArray.length;i++)
Mark M.
Content providers are for inter-application data sharing
there are a few technical spots where you need to have an internal content provider (e.g., search suggestions)
but otherwise, it just adds overhead
Sebastian
thanks
Mark M.
Gene: neither -- use for (String s : myArray) {}
9:05 PM
Mark M.
(substitute the right type for String in the above)
Gene R.
thanks!
Sebastian
i am building in eclipse right now and the project i have doesn't have a build.xml. i would like to build from the command line. can i generate the build.xml file ? ( sorry if that was covered in your books, i am not very far in yet)
Mark M.
run the following command:
android update project -p ...
where ... is the directory where the project resides
it will create build.xml for you
Sebastian
great excellent, i will try that out
9:10 PM
Gene R.
I am a bit confused as to what method of Threading to use? I currently create a background thread and communicate with the UI thread via Messages and Handlers. I noticed you mentioned that the Asynctask is easier to use as Android handles much of the maintenance. Is that the only difference between the two methods? Is one method better suited to long running background tasks than the other?
Mark M.
that depends
how do you define "long running"?
milliseconds?
seconds?
minutes?
hours?
millenia?
Gene R.
long running as in minutes to hours. But only running when the Activity is on screen and viewable.. (ie. not a service).
Mark M.
that's not a good pattern for AsyncTask
AsyncTask is more for things that will take seconds to run
your existing approach is probably better
Gene R.
Is there something inherent to AsyncTask that makes it more suitable for tasks that last only a few seconds?
Mark M.
yes
it uses a thread pool, for one
and queues
you would use AsyncTask for, say, downloading thumbnails to populate in a ListView
Gene R.
I see. Thank you for the clarification!
Mark M.
if you're careful, you could use AsyncTask for things that take longer, but it won't buy you much
9:15 PM
Gene R.
One more question. When Android puts my Activity to sleep, am I correct to assume all my instantiated objects will still be in memory when it wakes up my Activity? Can I still assume that if my task is killed and then re-started by the system?
Mark M.
define "to sleep"
do you mean onPause()?
or do you mean "the CPU went to sleep and the screen went dark"?
Gene R.
by sleep, I mean perhaps the user pressed the back button and onPause() is called.
Mark M.
well, if the user presses BACK, your activity is destroyed
Brian C.
has entered the room
Mark M.
if the user presses HOME, then it will be "put to sleep", insofar as it will hang around in memory for a while
your activity may still be destroyed sometime later, though, to free up RAM
if it is, you will be able to save data via onSaveInstanceState() and get that data back later
however, if the user quickly returns to your activity, they will see the currently-running instance
howdy, Brian!
Gene R.
i see. I'm still trying to wrap my head around the activity lifecycle. So if onPause() is called, all my instantiated objects are still in memory, but if onStop() my instantiated objects are destroyed?
Mark M.
no
Brian C.
hi, Mark.
Mark M.
if onDestroy() is called, all your objects are destroyed, for that activity
Gene R.
I see.
9:20 PM
Mark M.
onPause() means "no longer in the foreground"
9:20 PM
Mark M.
onStop() means "no longer visible on the screen"
onDestroy() means "no longer in the land of the living"
Gene R.
So, it is safe to assume my instantiated objects are still in memory until onDestroy()?
Mark M.
yes
well
mostly
there is a chance your process will be terminated, if Android needs RAM quickly, or thinks your process has lost its marbles, or something
in that case, onDestroy() is not called (though onStop() will have been, some time earlier)
Gene R.
when does onSaveInstanceState() get called?
Mark M.
roughly around the time of onPause()/onStop()
Gene R.
Thank you very much for your answer!
Mark M.
Brian: we're running out of time for this hour -- did you have a question?
Brian C.
no, just wanted to check things out
9:25 PM
Sebastian
is 1.5 what most people target nowadays as minimum supported version?
Mark M.
compared to 1.1, yes
some are leaving 1.5 behind because they need 1.6 features (e.g., better screen size support)
if possible, I'd aim to support 1.5 for another few months
for example, the Motorola BACKFLIP was just released, and it ships with 1.5
Sebastian
i see, thanks
Gene R.
Mark, thank you for answering our questions. I look forward to your next office hours!
Mark M.
you're very welcome
Sebastian
well, thanks again... all the questions and answer were very informative
Gene R.
has left the room
Mark M.
that's about all the time we have for this hour
I'll post the chat transcript shortly
Sebastian
bye
9:30 PM
Sebastian
has left the room
9:30 PM
Brian C.
has left the room
Mark M.
turned off guest access

Thursday, March 4

 

Office Hours

People in this transcript

  • Brian Cooley
  • Gene Ruebsamen
  • Mark Murphy
  • Sebastian