Office Hours — Today, November 14

Thursday, November 12

Nov 14
3:50 PM
Mark M.
has entered the room
Mark M.
turned on guest access
3:55 PM
RP
has entered the room
Mark M.
hello, RP!
how can I help you today?
RP
Hi Mark. I have a question about communication between fragments
View paste
I have a TextView in one fragment which I want to get updated when clicking a button in another so that when I move to that fragment (via ViewPager) I see the updated text.
I cannot seem to get the text to update when I use setText even though the debugger shows I’m executing the update code when I click the button in the first fragment. 
I can get the text to update if I connect it to a button within the same fragment.
Do you know how what I need to do to get this to function?
Mark M.
um, well, one fragment should not have a reference to another fragment's views
those should be more loosely coupled
RP
I'm doing it via the activity
Mark M.
ok, then, have you stepped through this code in a debugger, or used Log statements, or anything to determine what code is and is not executing?
4:00 PM
Steve
has entered the room
Mark M.
(hello, Steve -- I will be with you shortly!)
Steve
Great, thanks Mark.
RP
Yes and I do see the setText being executed while clicking the button in the first fragment but when I move to the second fragment I see nothing has chnaged.
Mark M.
then either you are not setting the value in setText() that you think, or you are setting it on the wrong TextView, or something
or, you are resetting the text on the TextView before you go to look at it
let me take a question from Steve, and I will be back with you shortly
Steve: your turn! do you have a question?
RP
But as I said in the original post I can make it change if I connect to a button in the second fragment. To be clear the commend is issued while I'm in the first page so I'm hoping to see the change when I sroll to the second page. Do I need an explicit view update
Mark M.
(RP: no, there should be no difference)
Steve
Yes. I've been programming Android for a few months and have a basic question about how I should implement networking for an app I'm working on.
I can tell you a bit about the requirements.
4:05 PM
Mark M.
well, I need something -- I can answer questions, but you haven't exactly asked a question yet :-)
Steve
What I need to do is the following.
The app uses a Rest backend. I'm currently using AsyncTasks with HttpUrlConnection within fragments.
An issue is that I'd prefer making the network requests independent of the fragment lifecycle.
I'm wondering if using a service to make network requests would be a better approach.
Mark M.
that's impossible to answer in the abstract
in general, using an AsyncTask means "if this does not complete, I'm cool with that"
Steve
Right.
Mark M.
use it for things like downloading information for display, where you're not trying to hold onto that data long-term locally
Steve
Ok.
This is data that I need to hold onto.
Mark M.
an IntentService is more apropos for networking operations that you really want to ensure complete, but are still transactional (i.e., will take a few seconds at most)
Steve
Great. That's something I was looking at.
Mark M.
if the networking operations might take an extended period of time (15+ seconds), you're now looking at using my WakefulIntentService or doing something else to help ensure the device will stay awake long enough for you to complete your work
Steve
So I'm wondering how to go about implementing that.
Mark M.
um, generally speaking, replace the AsyncTask with an IntentService
doInBackground() becomes onHandleIntent()
4:10 PM
Steve
Great. That's very helpful.
Mark M.
getting results back to the UI layer becomes more complicated -- I usually use greenrobot's EventBus
but there are alternative solutions
this sort of stuff is covered in the chapters on services and event buses in the book
Steve
I saw you used EventBus in an example in your book.
Mark M.
let me take a question from RP, and I'll be back with you in a bit
RP: back to you! do you have another question?
Steve
Sure, thanks Mark.
RP
No that was it. I guess I need to go back and re-examine the code if it is nothing obvious. I just wanted to confirm I do not need an explicit view update to make this work
Mark M.
no, calling setText() should work, regardless of whether the TextView is presently visible
that being said, I haven't tried your scenario recently
RP
okay thanks Mark. That does it for me today.
Mark M.
but I'm not aware of any manual invalidate() calls or anything that you would need to do
OK, if you happen to come up with another question while you're signed in to the chat, let me know
Steve: back to you!
RP
I did try invalidate and postInvalidate and they don;t help.
Mark M.
(RP: then it really feels like the TextView has the wrong text, either because you're calling setText() on the wrong object, or calling it again when the page is coming back into view)
Steve
Regarding EventBus, my concern is that if I use an external library there could be issues when a new version of the Android OS comes out.
4:15 PM
Mark M.
Android takes great pains to maximize backwards compatibility
there are apps written for Android 1.0 that work fine on Android 6.0
and modern Android development is impractical without third-party libraries
(at least IMHO)
that being said, you're welcome to use LocalBroadcastManager, as that's from the Android Support libraries, and therefore is maintained by Google
it is in the support-v4 library
it too is an event bus
compared to greenrobot's EventBus, it is more cumbersome and less flexible
but, it's Googly
Steve
My concern was based on what some experienced Android developers had said when I asked them their advice. For that reason, they raised concerns about using Retrofit. But it sounds like you don't think this is an issue.
Mark M.
um, I got 99 problems, but that ain't one of 'em
Steve
Ok.
Mark M.
I mean, there's always the possibility of incompatibilities cropping up
but to be blunt, that's as likely from Google's own code as it is from third parties
Steve
Sure, that's very helpful. Are there examples in your book that would be particularly relevant to what I want to do?
Mark M.
well, as I mentioned, the chapter on event bus alternatives compares and contrasts three event bus implementations (greenrobot, Square's Otto, and LocalBroadcastManager), using the same sample app implemented three times
4:20 PM
Mark M.
the EmPubLite tutorials use an IntentService for downloading book updates
(actually, a WakefulIntentService, IIRC)
Steve
Great. I'll check them out.
Mark M.
I don't have a lot of samples that go the IntentService route, mostly because for the "business requirements" of my trivial book samples, it's not really needed
so I'll have bare threads and an event bus, or sometimes AsyncTask
the book is mostly designed to describe capabilities and show their basic operation, but it's therefore a mile wide and an inch deep in terms of example complexity
Steve
That's an issue I've been having trying to learn Android. A lot of the examples I've found leave out stuff that you have to do in real-life, like deal with screen rotations.
Mark M.
well, a reasonable number of my samples handle configuration changes correctly
and if any don't (and aren't described explicitly as missing this), that's a fine candidate for the Book Bug Bounty :-)
Steve
This has been very helpful, Mark. I'll take your suggestions and will look at the examples you mentioned.
I saw somewhere that it's possible to get a transcript of the chat. Can you please tell me how do that?
Mark M.
if either of you have another question, go right ahead
all of the chats get archived there
4:25 PM
Mark M.
this transcript will show up shortly after the chat ends
RP
I'm good for now. Thanks for the help today Mark.
Steve
Great! Thank you so much, Mark! I don't have any more questions now.
Mark M.
you're both very welcome
RP
has left the room
Steve
has left the room
4:55 PM
Mark M.
turned off guest access

Thursday, November 12

 

Office Hours

People in this transcript

  • Mark Murphy
  • RP
  • Steve