Office Hours — Today, April 11

Tuesday, April 9

Apr 11
7:25 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:30 PM
Brett G.
has entered the room
Mark M.
howdy, Brett!
how can I help you today?
Brett G.
Hi Mark!
First, thanks for the Awesome book! I really appreciate how fresh you keep it!
Mark M.
you are very welcome
Brett G.
I have a follow up from a question you answered for me on SO this week: http://stackoverflow.com/questions/15907664/how...
Mark M.
I have it up now
what you want is very reminiscent of the EU4You example
Brett G.
I'm implementing this single activity app with individual fragments for each screen.
Mark M.
OK
Brett G.
I'm using your loaderex
Mark M.
SQLiteCursorLoader?
7:35 PM
Brett G.
and I'm a having some trouble figuring out how to run the loader from within a fragment, instead of from within an activity
yes sir
Mark M.
first, what is your android:minSdkVersion?
Brett G.
grabbing some code from my remote desktop now
Mark M.
(IOW, are you using the fragment backport, or regular API Level 11 fragments?)
Brett G.
support library, absherlock
and you're loaderex library
Mark M.
OK
getLoaderManager() is available on Fragment
so, pretty much it should work just like it does on an activity
Brett G.
oh wow, I bet that's my prob. I was calling getSherlockActivity and accessing from the Activity
Mark M.
except that you probably implelement the LoaderCallbacks<Cursor> on the fragment
Brett G.
was having trouble with back/forward navigation with that setup
Mark M.
I'm not convinced that working with LoaderManager directly in the fragment (vs. getting it from the activity) will help in that regard
what specific trouble were you encountering?
Brett G.
I see getLoaderManager, but not getSupportLoaderManager
in the SherlockFragment
Mark M.
getLoaderManager() on the support.v4 Fragment returns a support.v4 LoaderManager
hence, there is no dedicated getSupportLoaderManager()
Brett G.
OK, I'll change it out now and see if that fixes my problem, here's a code snippet of what I'm doing in case you see something. It works fine on first load, but if I hit back and the come back to the fragment again, the changeCursor call doesn't have any effect
7:40 PM
Brett G.
View paste
LoaderManager lm =  getSherlockActivity().getSupportLoaderManager();
		
		if(lm.getLoader(0) == null){
			lm.initLoader(0, null, this);
			//load uncategorized verses asynchronously
		}else{
			lm.getLoader(0).forceLoad();
		}
		
		if(lm.getLoader(1) == null){
			lm.initLoader(1, null, this);
			//load uncategorized verses asynchronously
		}else{
			lm.getLoader(1).forceLoad();
		}
View paste (8 more lines)
@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		switch (loader.getId()){
		case 0:
			//collections
			iMergeNewSectionIndex = cursor.getCount();
			daCategories.changeCursor(cursor);
			//daCategories.swapCursor(cursor);
			break;
		case 1:
			//verses
			daUncategorizedMemoryVerses.changeCursor(cursor);
			//daUncategorizedMemoryVerses.swapCursor(cursor);
			break;
		}
...
Mark M.
you want to use swapCursor() with Loaders
use the CursorAdapter backport if needed: http://developer.android.com/reference/android/...
Brett G.
OK, I'll change that out. I did try that a few minutes ago and I'm still having the trouble with the listview not loading after swapping the cursor
Mark M.
beyond that, I am not quite certain what is happening to your fragment mix when you press BACK
are you using addToBackStack() to handle that, or are you doing something manually (and, if so, what)?
Brett G.
Yes, I'm using FragmentTransactions as your recommended and adding to back stack
Blake M.
has entered the room
Brett G.
and I just popbackstack if the home/up button is pressed
Mark M.
(BTW, howdy, Blake -- be with you shortly!)
Blake M.
Hi ya'll. New to commonsware, so I thought I'd just stop by and say hello
Mark M.
OK, and then how are you "returning" to the fragment?
are you saying that you are holding onto the Fragment instance somewhere yourself?
Brett G.
just press the button that "transacts" to it again
7:45 PM
Mark M.
or are you creating a new fragment each time the user taps on the list entry?
7:45 PM
Brett G.
Here's the onclick code:
getSherlockActivity().getSupportFragmentManager().beginTransaction().addToBackStack(null).setCustomAnimations(R.layout.slide_in_right,R.layout.slide_out_left, android.R.anim.slide_in_left,android.R.anim.slide_out_right).replace(R.id.frameRight, new FragMyVerses(), "FragMyVerses").commit();
Mark M.
right, so you are creating a new fragment each time
Brett G.
Yes
Mark M.
you would wind up with a new ListView each time regardless, as onCreateView() should be called fresh again anyway
Brett G.
I would be fine with that.
Mark M.
assuming that the loaders are handing you an actual Cursor with actual data, I do not know why the contents would not appear after swapCursor()
Brett G.
the cursor has data if I put a breakpoint there
but after swapping, the listview is empty
only on subsequent loads of the fragment, it works fine on the first load
Mark M.
I do not know what would be causing that
Brett G.
trying it now with getLoader from fragment instead of activity to see if that helps
Mark M.
double-check to make sure the CursorAdapter is holding the right Cursor post-swap
and perhaps poke around in Hierarchy View to see if it is a rendering issue, more than an adapter issue
meanwhile, let me take a question from Blake, and I'll be back with you shortly
Blake: howdy, sir -- do you have a question?
Blake M.
Hi Mark. No question, really. Just stopped by to see how this works.
Mark M.
ah, OK
it's mostly for Q&A, more than a "hangout" type chat, though you are welcome to stick around if you like
attendance varies from zero to a half-dozen
7:50 PM
Blake M.
ah! ok, fair enough.
Mark M.
I try to take everyone's questions in turn, so I don't get lost in a bunch of parallel chat threads
Blake M.
hey, actually, do you happen to know what a Theme really is? A style, of course, and visible where android Styles are not. But how does it work?
Mark M.
themes aren't my strong suit
Blake M.
me too. ok then, see you around.
Mark M.
my interpretation of a theme is: a style that is applied to a whole activity/app, and that can affect things beyond an individual widget (e.g., window behavior, like whether there is an action bar)
someday, I need to spend quality time on the subject and add an Advanced Styles and Themes chapter to my book :-)
Blake M.
yeah. I think so. ... and you can "back-fit" values into it, I think.
:-)
Blake M.
has left the room
Mark M.
OK, Brett: do you have another question?
7:55 PM
Brett G.
thanks for your patience, i'm trying to test the change over a slow remote desktop connection
Mark M.
no worries, chime in if another question comes to mind
Brett G.
is the first code sample I pasted the correct way to initialize the loader, checking for null and the either initLoader or forceLoad?
Mark M.
in your case, probably
Brett G.
[and then either initLoader or forceLoad?]
Mark M.
I haven't experimented with your scenario
to be honest, I'm not a Loader fan
Brett G.
I had to change it to that method to get it to reload properly on 'back' navigation to the fragment
would you just use asynctask?
Mark M.
yes, or something along those lines
8:00 PM
Brett G.
what would I be looking for in the call hierarchy?
Mark M.
I am sorry, but I do not understand your question
Brett G.
you mentioned looking for a rendering issue there
Mark M.
not call hierarchy, Hierarchy View
lets you inspect the widgets and containers of your running activity in an emulator
basically, confirm that the ListView truly is empty
i.e., has no children
Brett G.
sounds like something helpful I need to familiarize myself with
Mark M.
it's a perspective in Eclipse, or available from the standalone monitor tool if you are not using Eclipse
I have coverage of it in the book
Brett G.
sweet, loading it up now
8:05 PM
Brett G.
I'm setting setRetainInstance(true); on all of my fragments, is that appropriate in my scenario?
for configuration changes
Mark M.
Loaders will redeliver your Cursor to you post-config change
hence, in theory, you should not need setRetainInstance(true) for that
and I seem to recall that Dianne Hackborn was opposed to using setRetainInstance(true) with Loader-based fragments
Brett G.
Gotcha, I saw a post from her today that mentioned that which is why I asked :)
didn't know if it would apply to my situation, so glad to have a confirmation.
But for my fragments without a loader, I'll continue to "setRetainInstance(true);", correct?
Mark M.
only if you need it
8:10 PM
Brett G.
If I don't have that set, and I'm 5 pages (fragments) into my navigation, won't I lose my place when the activity reloads? Or is it smart enough to reload my fragment stack?
Mark M.
the back stack is independent of setRetainInstance(true), if that's what you mean
all of your fragments always get recreated on a configuration change (except those that are retained)
Brett G.
hmm, I'll have to go change my code back. I thought I remembered it landing me back at the first fragment after a screen rotation
Mark M.
make sure that, on a configuration change, you don't accidentally force yourself back to the first fragment by running transactions
Brett G.
ah, gotcha
I think you hit the nail on the head :)
Mark M.
"the nail says, 'ow!'"
:-)
Brett G.
I prob did have the code to check for existence of them in place yet back then
I hadn't ready the Fragments section of your book at that point :)
[read]
8:15 PM
Brett G.
well so much for testing this, I'm all of a sudden getting the dreaded 'table not found' error on my database query all of sudden.
Mark M.
that's a little odd
Brett G.
haven't had that problem in days
back when I was setting up my database access singleton
Mark M.
unless you're wiping out internal storage yourself, your database should be around from previous runs
Brett G.
and copying the db to the wrong folder
Mark M.
"copying the db"?
Brett G.
I just fired up an emulator since I'm not in the office
Mark M.
you're shipping a database?
Brett G.
I use a pre-made db, yeah
Mark M.
are you using SQLiteAssetHelper for that?
Brett G.
I have a lot of library data user's can pick from
SQLiteOpenHelper
I'm guessing there's a better way :)
Mark M.
ah, you really want to consider using SQLiteAssetHelper: https://github.com/jgilfelt/android-sqlite-asse...
handles the package-a-database scenario nicely
I cover it in the book as well, with my own sample app
beats rolling your own solution
8:20 PM
Brett G.
I just picked up your book last night, so I plan to be much more familiar with the basics by our next chat session :) I've only made it through the fragments and config changes section at this point
Mark M.
it's a big book
Brett G.
I'm very excited about having everything in one place, and not having to filter through all of the "old" methods to find what's current
I really appreciate what you're doing!
Mark M.
pretty much everything was spruced up in a "reboot" of the book a year ago
Brett G.
I'm gonna check out and go help with the little one. Thanks so much for your time and insight!
Mark M.
each update, I try to refresh some existing material in addition to covering new topics
you are very welcome
BTW, these chats are archived at http://commonsware.com/office-hours/
this chat's transcript will show up a few minutes after the chat ends
Brett G.
Good Night! Great! That's helpful to know!
8:30 PM
Mark M.
that's a wrap for today's chat
next chat is Wednesday at 10am Eastern
have a pleasant day!
Brett G.
has left the room
Mark M.
turned off guest access

Tuesday, April 9

 

Office Hours

People in this transcript

  • Blake Meike
  • Brett Golson
  • Mark Murphy