Office Hours — Today, June 27

Tuesday, June 25

Jun 27
7:25 PM
Mark M.
has entered the room
Mark M.
turned on guest access
EGHDK
has entered the room
Mark M.
hello, EGHDK
how can I help you today>
EGHDK
View paste
Rapid fire questions:
When I call finish() why does code after it still work? Example finish(); startActivity(intent);
Mark M.
why wouldn't it
?
it's a method call
EGHDK
Doesn't finish end the activity?
Mark M.
finish() *schedules* the end of the activity
EGHDK
...?
How does it know when to end then?
Mark M.
moreover, short of an exception, there's no way for it to skip the rest of the statements
finish() puts an event on the message queue processed by the main application thread
when that message bubbles to the top, the activity begins the shutdown process (onPause(), etc.)
EGHDK
Puts an event on the "end" of the message queue?
Cool. Gotcha.
Mark M.
presumably at the end, though I haven't looked
it doesn't matter vis a vis your question
EGHDK
So for example, I'm in MainActivity, and I want to finish MainActivity because I want it to start again. Is it proper to call startActivity(intent); before finish()?
Or the other way around? Or does it not even matter?
Mark M.
that's the typical approach, though AFAIK the reverse order would still work
EGHDK
Okay, next question. I've been working a lot with databases and reading a lot into your SQLite tutorials.
7:30 PM
EGHDK
I'm having trouble with the Async stuff, but for now I'm just trying to do simple CRUD actions.
Mostly because there are many ways to do CRUD.
I followed your technique and simply extended a SQLiteOpenHelper, but a lot of people create a Data class, and then create a class inside of that. Is any way better than another?
If that makes sense...
Mark M.
what's "a Data class"?
EGHDK
Just like AppData.java and then within that they have a class that extends SQLiteOpenHelper.
Mark M.
I can't really comment on what's better
that would require me to have seen examples of your pattern and remember what they look like
suffice it to say, there are any number of ways to organize the CRUD code
and I'm not aware of any specific "better" or "worse" approach among them, so long as they're doing the work asynchronously by one means or another
7:35 PM
EGHDK
Well I'm basically just a bit confused, because in this video https://www.youtube.com/watch?v=c5n90wiv75M if you pause at 16:26 you see the code I'm talking about.
Mark M.
again, I cannot really comment on it
EGHDK
Sorry for the link, idk if you "accept" that on the chat.
Mark M.
links are fine, though reviewing source code by YouTube video is not especially practical
EGHDK
Hahah true.
Mark M.
I doubt there's anything substantially wrong with what they're doing
EGHDK
Okay, so hopefully you can comment on this. I have a database with multiple tables. What would be the best way to set that up? One DBHelper class?
Mark M.
well, it's one SQLiteOpenHelper per database, regardless of the number of tables
in terms of where your CRUD methods go, I'd argue that it would depend a bit more on what sort of Java API you need
for example, if there are multiple tables, but you're converting the SQLite data to/from POJOs, there might be a single class responsible for persisting and loading the POJOs
EGHDK
Ideally, I'll be using about 2 or three tables constantly. SO I need to go back and look at your Async DB code.
Mark M.
if, OTOH, you are sticking more with Cursors as what you're loading and putting in the UI, then one DBHelper per table may make more sense
my database samples are mostly designed to show how to run the code
code organization techniques vary by project size and other characteristics that exceed the scope of my samples
7:40 PM
Mark M.
where there are functional aspects (e.g., threading), my samples are probably fairly good
EGHDK
I believe I looked through your samples a few times and I still had trouble on inserting into the database, outside of the DBHelper class.
Mark M.
I can't really comment on "trouble" without more specifics, sorry
EGHDK
In your sqlite section, you only show insert statements inside of the oncreate and inside of an async task.
Oh. Is that because it should only be done in OnCreate or inside of an async task! =)
?
Mark M.
certainly, inserts should be done in a background thread
EGHDK
I'm a genius.
Hahaha. It only took me the time to write down the question, and ask you the question, for it to actually hit me.
Mark M.
onCreate() of a SQLiteOpenHelper is hopefully being called on a background thread, by virtue of your calling getReadableDatabase() or getWriteableDatabase() on a background thread
EGHDK
That's why you don't show how to do it anywhere else. Aww man. Sweet.
7:45 PM
Mark M.
now, it doesn't strictly have to be an AsyncTask -- any sort of background thread would work
(e.g., IntentService)
EGHDK
Okay, couple more questions then. If I'm going to be accessing my database a lot should I open it once, and not close it often?
Mark M.
yes
EGHDK
Okay, cool. Next question. I do this quite often. I take a button and give it a white background, so it immediately loses that gray button color. It also loses touch feedback. Is there anyway to add touch feedback without creating a new file for it?
Mark M.
it's more that if you want to change the background, you cannot just call setBackgroundColor()
what makes a Button work like a Button is the background
which is a StateListDrawable
hence, the only way to really change the background color *and* keep it working like a Button is to replace the StateListDrawable with another StateListDrawable
where you change the images for the relevant states to be your desired color
EGHDK
I could've sworn I saw some xml attribute that will add that blue glow to any button. Or am I just imagining things?
Mark M.
AFAIK, you are imagining things
EGHDK
Maybe its the weather. Flash floods and such. You should be fine in PA hopefully!
Mark M.
BTW, I think the Android Holo Colors generator can help you with this
7:50 PM
EGHDK
Yeah thanks.
So I really don't use styles.xml
Should I start? I mean when I do web design, I always use styles.css. But I never feel the need to. I always just hard code changes to stuff in xml. Thats pretty bad right?
Mark M.
I wouldn't say it's "pretty bad"
Liam
has entered the room
Mark M.
in Web development, you often inline CSS style rules, for one-off divs and the like
where the CSS dedicated files come in is for styles you're repeating across multiple spots
(BTW, howdy, Liam -- be with you in a minute!)
for widgets that allow adjusting UI both via attributes and via styles, you can take a similar approach
Liam
Thanks Mark!
Mark M.
but for something like a button background, since you probably should be applying that same look for most/all buttons, a style would seem apropos
let me take a question from Liam, and I'll be back with you in a bit
Liam: hi! do you have a question?
Liam
View paste
First, thanks for your time Mark. I am working on releasing my first android application and are having two problems. 
First, I am implementing an options menu with the on create options menu.
EGHDK
Alright Mark, I'm out. That was the end of my list of questions after programming for today. Thanks again.
Mark M.
EGHDK: you're welcome!
Liam
It works great on most phones, but prior to api 11 it doesnt show up.
7:55 PM
Liam
I think it is not supported in earlier versions ( after checking the docs)
Mark M.
Liam: well, on newer Android devices, stuff that had been the options menu becomes part of the action bar
Liam
but i am trying to figure out a way to implement a work around
Mark M.
on older Android devices, you would press the MENU button to view the options menu
Liam
hmm yeah that is what i thought but when i hit the men button it seems to shut down
Mark M.
what is "it", and what does "shut down" mean?
Liam
Sorry, the app closes with a standard error code
Mark M.
what is "a standard error code"?
Liam
Application is not responding
Mark M.
OK, that means that you are doing something that is tying up the main application thread
and doing so for a substantial period of time
that may or may not have anything really to do with the options menu
as pressing on the MENU button may just be the trigger for Android to decide to raise the ANR, given the frozen app UI
Liam
Okay, that makes sense. So just to make sure I understand, there is nothing about the onCreateOptionsMenu or onCreateOptionsMenu that wouldnt work on older versions, right>
Mark M.
correct
8:00 PM
Mark M.
and, if you want a consistent action bar look-and-feel even on the older devices, use ActionBarSherlock
Liam
Okay, thanks. Yeah, I will check that out too.
Do you have time for another question?
View paste
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Mark M.
go right ahead -- the chat runs for another half-hour, and EGHDK asked a bunch of questions earlier :-)
Liam
Okay, sweet.
So I writing an app that calls the following flags to wake the phone up and keep it on
I also clear the flags but the phone does not go to sleep after
Mark M.
it will go to sleep after the normal timeout period
or when the user presses the power button
Liam
Yeah, for some reason that is being overriden
almost as if i were using the power manager's wake lock
Mark M.
you might try using setKeepScreenOn() on a View, instead of FLAG_KEEP_SCREEN_ON, and see if that improves matters
I have never tried clearing FLAG_KEEP_SCREEN_ON, and I pretty much always use setKeepScreenOn() (and android:keepScreenOn) rather than use that flag anyway
8:05 PM
Liam
Okay, i will give that a try. I am also calling home screen using this method. Any chance this is causing a problem?
View paste
					Intent startMain = new Intent(Intent.ACTION_MAIN);
					startMain.addCategory(Intent.CATEGORY_HOME);
					startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(startMain);
Mark M.
I doubt that you need the FLAG_ACTIVITY_NEW_TASK part
otherwise, I would not expect that to represent any sort of problem
Liam
Okay, thanks.
Mark M.
EGHDK: if you have another question, chime in
OK, if either of you have a question, chime in
8:10 PM
Liam
Mark- any way or program you recommend to test battery consumption?
Mark M.
that's seriously difficult without dedicated hardware
if you have $1000 lying around that you don't know what to do with, buy a Qualcomm MDP device and use Trepn
at the moment, we're lacking great options for specifically measuring battery use
Liam
Okay, yeah I will keep going with how hot the phone is then!
Mark M.
so, you wind up focusing on CPU and radio use, etc.
IOW, focus on the things that drain the battery, more so than the battery itself
Liam
Okay, thanks! Yeah, that is why i stopped used the PowerManager
That sounded like a big no no
Mark M.
if you mean WakeLocks, they are safe in short stints, but not the sort of thing you want to hold a long time
Liam
Yep, that is what I mean and thanks.
You are good!
Mark M.
I try
8:15 PM
Mark M.
and thanks!
Liam
Yep! If no one has any other questions... I am working on your setKeepScreenOn() implementation. Can I just attach to any view in my activity?
Mark M.
yes
Liam
Okay, cool.
8:25 PM
Mark M.
any last questions?
8:30 PM
Mark M.
OK, that's a wrap for today's chat
next chat is Monday at 10am Eastern
this chat's transcript will be posted shortly to http://commonsware.com/office-hours/
have a pleasant day!
Liam
Thanks!!
EGHDK
has left the room
Liam
has left the room
Mark M.
turned off guest access

Tuesday, June 25

 

Office Hours

People in this transcript

  • EGHDK
  • Liam
  • Mark Murphy