Office Hours — Today, September 13

Tuesday, September 11

Mark M.
has entered the room
Mark M.
turned on guest access
Sep 13
8:05 PM
Aaron
has entered the room
Aaron
hey Mark, I hope you are having a good week! I have 4 questions for you today. Here they are:
Mark M.
hello, Aaron!
go right ahead!
Aaron
View paste
1. What does the dot in RecyclerView.Adapter mean? Why isn't it called RecyclerViewAdapter?

2. I'm using android:background="?android:attr/selectableItemBackground" to animate clicks on RecyclerView rows, but the animation is very slow. It's only noticeable if the click is held on purpose for at least 1/3-1/2 a second or so, and it's a slow gray fade in, which doesn't seem to be described as one of the possible outcomes in your book. The current theme in my app is Theme.AppCompat.Light.DarkActionBar. Any idea what is happening and why?

3. Is there ever any reason besides making a chain, to create a bidirectional connection in a ConstraintLayout?

4. I am using SharedPreferences to store the user's login state (an OAuth access token and a refresh token.  the user is considered "logged in" if they are not null, and logged out when they are). I'm working on the login logic and running into a situation where I want to make some HTTP calls immediately after the user logs in, so basically I have one method writing into the SharedPrefs and then another one reading it right after. Problem is with using apply() as sometimes the write doesn't finish before the next method tries to read. Sh
...
Mark M.
1. Adapter is a static class defined inside of RecyclerView. If you look at the RecyclerView source code, you will find Adapter defined inside of it.
2. That does not sound familiar, sorry.
3. Off the top of my head, I cannot think of any, though ConstraintLayout is a complex beast, and so I cannot rule out other scenarios.
4. You could consider the SharedPreferences to be purely a backing store, with some object holding a cached value for your ongoing use.
8:10 PM
Mark M.
I'm guessing you'll want clarification on some of those :-)
Aaron
1-3 are clear, my main followup question would be, in general, which approach would you suggest for 4 and why? The amount of data being written is very tiny and shouldn't block the thread for very long. I guess the question is just whether there is a best practice, and how naughty it is to fall back to commit()
Mark M.
well, ideally, you don't call commit() on the main application thread
it's not terribly evil (like, say, network I/O on the main application thread)
but it's not that hard to avoid, either
Aaron
OK, that answers it, I will figure out another way
Mark M.
so, I'd have some sort of singleton (a "repository" in the current popular terminology) that represents your app's access to this data
that singleton in turn writes to the SharedPreferences and gets its initial value from those same SharedPreferences
8:15 PM
Mark M.
but the singleton also caches the value, so it can asynchronously write while still handing over that value on other threads (e.g., main application thread) as needed
Aaron
OK that makes sense
let me check to see if I have any other questions, as I just noticed it is 5 days until the next chat
Mark M.
yeah, sorry, I'm tied up this weekend
Aaron
no worries
ok I have a couple other quick ones
Mark M.
fire away!
Aaron
there have been a couple of times I wanted to find the source code for some class in the framework, or the support library, and Google was not always helpful for pulling it up. In general can you describe the process for tracking down any given source file? I'm not even sure which website to use, it seems like Google has some code on github, and some on a website called googlesource.com... not sure where to begin
Mark M.
first, in the SDK Manager, you have the options for downloading the source code for different API levels, as part of downloading the other SDK bits for those API levels
Aaron
ooh, ok
Mark M.
if you do that, and you're using Android Studio, just put the text cursor in a keyword of interest and press Ctrl-B
8:20 PM
Mark M.
and the source will be opened in another editor tab
works for libraries too, if the library packages source code along with the compiled bits
Aaron
ok great
Mark M.
beyond that, I tend to use the GitHub mirror of the AOSP source code
so, for example, this will contain directories that look familiar: https://github.com/aosp-mirror/platform_framewo...
different releases are tagged, so you can view the source for specific OS releases
and if you need full-text search, use http://aosp.opersys.com/
so, lots of options
Aaron
great, another question:
8:25 PM
Aaron
some of the HTML fields I get back from the reddit API are unescaped and they also contain some special entity characters, for example: <, >, and & replaced with &lt;, &gt;, and &amp;
Mark M.
Html.fromHtml() will clean those up
you'll see that I use that in the various book examples that show lists of questions from Stack Overflow, because their API does the same thing
Aaron
right, but the weird thing is, I have to run it through fromHtml twice to properly display the content formatted in a TextView
as in, piping the output of the first call, back into another fromHtml call
I am guessing it has something to do with the special entities?
Mark M.
that seems strange
no, a single call would suffice
Aaron
yes, I thought it was strange as well
ok, I don't have any more data handy, I just wanted to see if that rang a bell
Mark M.
so, if I get a question title that has "something something &amp; something", a single call to Html.fromHtml() converts that into "something something & something", which TextView can display
requiring two Html.fromHtml() calls implies that your data somehow wound up double-encoded
"something something &amp;amp; something"
so the first Html.fromHtml() turns that into "something something &amp; something"
and the second one gives you "something something & something"
so, either use logging or breakpoints to see what the data is, and see if that double-encoding is indeed what's happening
Aaron
hmm OK I'll take another look
ok thanks! Have a good weekend!
Mark M.
if it is, I don't know why it would be, but you might be able to track down the rationale, or if it is something about how you're using the API
you too!
Aaron
will do
8:30 PM
Mark M.
the next chat is Tuesday also at 7:30pm US Eastern
I'll have the transcript of this one up at https://commonsware.com/office-hours/ shortly
see ya!
Aaron
has left the room
Mark M.
turned off guest access

Tuesday, September 11

 

Office Hours

People in this transcript

  • Aaron
  • Mark Murphy