Mar 9 | 8:20 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Mar 9 | 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.
|
Gene R. |
Do I need to add some code to get the preference screen to display the current application settings/preferences?
|
Mar 9 | 8:50 PM |
Mark M. |
not usually
|
Mark M. | |
Mark M. |
those are the preference example from _The Busy Coder's Guide to Android Development_
|
Mark M. |
they too use the onResume() approach
|
Mark M. |
they do not exhibit this problem, AFAIK
|
Mark M. |
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
|
Mark M. |
PreferenceManager.getDefaultSharedPreferences()
|
Mark M. |
that is what you should be using
|
Mar 9 | 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?
|
Gene R. |
other than the call to addPreferenceFromResource(...)
|
Mark M. |
you don't instantiate the Preference class
|
Mark M. |
oh, you mean PreferenceActivity
|
Mark M. |
no, that's all that is needed
|
Gene R. |
I'm sorry, I mean in the definition of the PreferenceActivity class.
|
Gene R. |
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?
|
Mark M. |
thanks!
|
Mark M. |
the Warescription is definitely the better deal, if you don't need it printed
|
Mark M. |
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
|
Mark M. |
Sebastian: thanks!
|
Mar 9 | 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.*
|
Mark M. |
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
|
Mark M. |
there are a few technical spots where you need to have an internal content provider (e.g., search suggestions)
|
Mark M. |
but otherwise, it just adds overhead
|
Sebastian |
thanks
|
Mark M. |
Gene: neither -- use for (String s : myArray) {}
|
Mar 9 | 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:
|
Mark M. |
android update project -p ...
|
Mark M. |
where ... is the directory where the project resides
|
Mark M. |
it will create build.xml for you
|
Sebastian |
great excellent, i will try that out
|
Mar 9 | 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
|
Mark M. |
how do you define "long running"?
|
Mark M. |
milliseconds?
|
Mark M. |
seconds?
|
Mark M. |
minutes?
|
Mark M. |
hours?
|
Mark M. |
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
|
Mark M. |
AsyncTask is more for things that will take seconds to run
|
Mark M. |
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
|
Mark M. |
it uses a thread pool, for one
|
Mark M. |
and queues
|
Mark M. |
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
|
Mar 9 | 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"
|
Mark M. |
do you mean onPause()?
|
Mark M. |
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
|
Mark M. |
your activity may still be destroyed sometime later, though, to free up RAM
|
Mark M. |
if it is, you will be able to save data via onSaveInstanceState() and get that data back later
|
Mark M. |
however, if the user quickly returns to your activity, they will see the currently-running instance
|
Mark M. |
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.
|
Mar 9 | 9:20 PM |
Mark M. |
onPause() means "no longer in the foreground"
|
Mar 9 | 9:20 PM |
Mark M. |
onStop() means "no longer visible on the screen"
|
Mark M. |
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
|
Mark M. |
well
|
Mark M. |
mostly
|
Mark M. |
there
is a chance your process will be terminated, if Android needs RAM
quickly, or thinks your process has lost its marbles, or something
|
Mark M. |
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
|
Mar 9 | 9:25 PM |
Sebastian |
is 1.5 what most people target nowadays as minimum supported version?
|
Mark M. |
compared to 1.1, yes
|
Mark M. |
some are leaving 1.5 behind because they need 1.6 features (e.g., better screen size support)
|
Mark M. |
if possible, I'd aim to support 1.5 for another few months
|
Mark M. |
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
|
Mark M. |
I'll post the chat transcript shortly
|
Sebastian |
bye
|
Mar 9 | 9:30 PM |
Sebastian | has left the room |
Mar 9 | 9:30 PM |
Brian C. | has left the room |
Mark M. | turned off guest access |