Office Hours — Today, August 2

Tuesday, July 31

Aug 2
7:20 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:30 PM
Aaron
has entered the room
Mark M.
hello, Aaron!
how can I help you today?
7:35 PM
Aaron
Hey Mark. I have 4-5 questions I've saved up, I'll go one at a time in ascending order of difficulty
1. pages 514-523 of Busy Coder's Guide says data in a bundle persists across process terminations, and that Android's default implementation of onSaveInstanceState() automatically stores obviously user mutable data like EditText into the bundle without any action on the developer's part. But when I create an empty activity project with just an EditText, text I type in does not survive terminating the app with the back button. This seems at odds with the book's information. What am I missing?
Mark M.
BACK destroys the activty
onSaveInstanceState() is no longer involved, as you destroyed the logical instance of the activity
onSaveInstanceState() is for configuration changes (e.g., screen rotation) and short-term process termination (e.g., user presses HOME, then returns to the app 20 minutes later)
Aaron
Ok, got it
Ok it seems there is a distinction between process termination and activity destruction that I need to research further.
Mark M.
they are not really related
see the "Android's Process Model" chapter (starting on page 421 of Version 8.12 of the book)
Aaron
Got it.
2. Why does EmPubLite use the 3rd party MaterialTabs library instead of 1st party TabLayout? Is the point just to demonstrate using an external library or is there some other reason?
7:40 PM
Mark M.
TabLayout requires appcompat, as it is part of the Design Support library; EmPubLite was set up to not use appcompat
Aaron
Ok, should have realized that. Got it
3. Regarding the StackOverflow with Retrofit example, in that code you have the fragment itself implement the Callback. What's the best practice when you have multiple calls in your Retrofit interface? Java doesn't allow implementing one interface with multiple generic types. I found some example code where the implementation is done by creating a new Callback object inside enqueue() but this seems sloppy... Best alternative?
Mark M.
actually, anonymous inner classes (or their lambda expression equivalents) are fairly common
or, you could hook up RxJava and the RxJava call factory for Retrofit, and use RxJava chains
you're certainly welcome to create dedicated Java classes for those callbacks as well, if you prefer, but I suspect that is relatively uncommon
Aaron
Ok, I'm probably not ready to learn RxJava quite yet, but good to know that the inner classes are acceptable.
7:45 PM
Aaron
4. Having finished most of your tutorials, the app I'm working on now is a Reddit client performing REST calls through Retrofit. First I create a retrofit object with the necessary base URL to get my OAuth token, but thereafter the base URL changes to Reddit's base API URL. Do I create a second retrofit object with the second base URL, or attempt to change the base URL of the first one, or do I do something altogether different? Best practice?
Mark M.
I have not worked with Reddit's API personally. But it feels like there are effectively two APIs: the OAuth one and the "real" one. In that case, I would have two separate Retrofit interfaces and two separate Retrofit objects.
they might share a common OkHttpClient, to use a common connection pool, thread pool, etc.
7:50 PM
Aaron
Ok, I saw some sample code online, I forget the exact method used but I believe they were specifying their own Http Client for the retrofit instance, which is something that isn't demonstrated in your book. Is that what you mean, manually specifying the client, or are you referring to something that would be performed automatically behind the scenes? I just want to make sure I understand well enough to research the same thing you are describing
Mark M.
"Is that what you mean, manually specifying the client" -- yes, via setClient()
Aaron
Might be a poorly formed question on my part, this is all new to me
Ok
Will look into it.
Mark M.
Aaron
Great :)
That's it for my questions. The final issue, which I am not sure is within scope of your support here, is a bug I cannot figure out which is actually related to the anonymous inner classes I created for my callbacks. I have tried to debug it without much luck. It would certainly be helpful to get your input, but I understand if reviewing more in depth code is outside what you can offer for the chat, no problem if so
Mark M.
if you are willing to have your code be public, I can look at it
7:55 PM
Mark M.
otherwise, I can't see it :)
Aaron
Sure thing. Let me commit the latest code to my Github. Will post the link momentarily
8:00 PM
Mark M.
is there something specific that I should be looking at?
Aaron
The code in question is in AllPostsFragment, which is a fragment that is handed to RedditFragmentPagerAdapter for use in a ViewPager (which currently only has one page)
In onViewCreated(),
I build the first retrofit instance for obtaining the OAuth token
And implement an anonymous inner class for the callbacks.
Mark M.
yup, I see it
Aaron
But it appears that none of the code executes, neither success nor failure cases
Mark M.
um, OK
and you are sure that you are creating the fragment and onViewCreated() is being called? for example, "marco" is getting logged?
Aaron
Yes. Marco and polo are both logged
And additionally,
If I pull the code out of the inner class and have the fragment implement the callback the way you do it in your book, it works fine
But when I put it in the anonymous inner class, it doesn't seem to be executed at all.
Mark M.
OK, that's strange
Aaron
I tried stepping through it with the debugger but just not experienced enough to figure it out
So yeah.
8:05 PM
Aaron
Not sure what is going on
If this would be a better question to post to Stackoverflow or something, I can do that
Mark M.
I use the anonymous inner class callback pattern with Retrofit in some of the book examples, so it definitely works
oh
you're not holding on to any of this
your Retrofit instance and your RedditInterface instance are both local variables
so as soon as onViewCreated() returns, those variables go out of scope
that's probably not helping your cause
Aaron
Ohh, that makes sense!
Mark M.
try making auth and riAuth fields on the fragment
and see if that helps
when the fragment itself is the callback, that object hangs around, and perhaps that's enough to keep it all working
8:15 PM
Aaron
Huh. I made auth and riAuth members of the fragment instead of local variables in onViewCreated(). The result is the same. Still nothing is logged from onResponse or onFailure.
Mark M.
hmmmmm...
I don't know where this is going wrong then, off the cuff
Aaron
Alright. Do you think posting a question on Stack Overflow is a reasonable next step?
8:20 PM
Mark M.
you might first compare and contrast your code with https://github.com/commonsguy/cw-omnibus/tree/v...
this is one of a few versions of my Stack Overflow client
the ones in the DataBinding directory use the anonymous inner class callback pattern -- see https://github.com/commonsguy/cw-omnibus/blob/v...
Aaron
Ok, will take a look. Thanks. I'll let you know if it turns out to be something interesting
I also have a few general points of feedback on the book now that I'm mostly through the tutorials, which I will send you via email in a bit
Thanks a lot for your help!
Mark M.
you're welcome!
Aaron
has left the room
8:30 PM
Mark M.
turned off guest access

Tuesday, July 31

 

Office Hours

People in this transcript

  • Aaron
  • Mark Murphy