Feb 21 | 7:25 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Steve S. | has entered the room |
Mark M. |
hello, Steve!
|
Mark M. |
how can I help you today?
|
Steve S. |
Hi, Mark!
|
Steve S. |
I have some questions about fragments. I
|
Steve S. |
Here's the first one:
|
Steve S. |
View paste
|
Mark M. |
sure
|
Steve S. |
ok. but it wouldn't be if setRetainInstance were called with true?
|
Mark M. |
it's even safe if you do call setRetainInstance(true), *if* you make sure that you *always* populate that field in onCreateView()/onViewCreated()
|
Mark M. |
the risk comes from filling in the field, then not replacing it on a configuration change
|
Mark M. |
you would get this from a lazy-retrieval pattern in a getter method, for example
|
Mark M. |
so getButton() { if (button==null) button=findViewById(R.id.button); return button; } is not safe
|
Steve S. |
i see. so onCreateView/onViewCreated are called regardless of whether setRetainInstance is set to true?
|
Mark M. |
correct
|
Mark M. |
you always get fresh views on a configuration change
|
Steve S. |
but onCreate is not called in a fragment if setRetainInstance is set to true?
|
Feb 21 | 7:30 PM |
Steve S. |
i mean on a rotation
|
Mark M. |
I'm pretty sure that's correct -- after all, the fragment instance is already created
|
Steve S. |
great. got it
|
Steve S. |
My next question:
|
Steve S. |
In your fragment example Dynamic, you make a call to findViewById() in onCreateView(). I have seen some suggestions that calls to findViewById() should be made instead in onViewCreated(). Does it make any difference?
|
Mark M. |
not especially
|
Mark M. |
I've been moving more to onViewCreated(); that's an old example
|
Mark M. |
you might inherit your onCreateView() (e.g., ListFragment) and only have an onViewCreated()
|
Steve S. |
ok
|
Steve S. |
i have another question pertaining to your Dynamic example:
|
Steve S. |
View paste
|
Mark M. |
you would use onSaveInstanceState() in a fragment for the same reasons you would in an activity
|
Mark M. |
there's really no difference
|
Steve S. |
ok
|
Feb 21 | 7:35 PM |
Steve S. |
my understanding is that persisting state in onStop() provides a more general solution to the problem of persisting state (though it's more work) since onSaveInstanceState() won't always be called. is that correct?
|
Mark M. |
um, I wouldn't characterize it that way
|
Mark M. |
first, persisting data to disk and onSaveInstanceState() aren't really the same thing
|
Steve S. |
ok. what are use cases for onSaveInstanceState()?
|
Steve S. |
ok
|
Mark M. |
suppose you are writing a to-do app
|
Mark M. |
or anything with a form
|
Mark M. |
the user is on the activity or fragment with the form
|
Mark M. |
the user partially fills in the form
|
Mark M. |
and a phone call comes in, or a text message comes in, or something else distracts them and takes them to another app
|
Mark M. |
the question is: what do you do with the partially filled-in form content?
|
Mark M. |
the user has not clicked "save" to say that they want to save it to the main data store
|
Mark M. |
a typical approach -- and the one that Android takes by default for most user-editable widgets -- is to stuff the form content into the saved instance state Bundle
|
Mark M. |
if the user is gone from the app for several minutes, the app's process may be terminated, taking the activity along with it
|
Mark M. |
if the user returns within 30 minutes, though, Android will re-create the activity and its fragments, handing each back their saved instance state Bundle
|
Mark M. |
the framework will repopulate the widgets that had been filled in previously
|
Feb 21 | 7:40 PM |
Mark M. |
so, when the user returns, it is almost as if the app had been around that whole time, even though it hadn't
|
Mark M. |
but, if the user leaves and does not return for quite some time, that state is discarded, on the grounds that the user probably doesn't care about that partially filled-in form a day later
|
Mark M. |
and for most apps, this works well
|
Mark M. |
most apps != all apps
|
Mark M. |
for an app where the user would be pissed if the partially filled-in form is discarded after, say, 45 minutes, you would need to save that data, possibly via a thread kicked off by onStop()
|
Mark M. |
and how you save that data, to take into account that it may be partial and the user never clicked "save", is up to you
|
Mark M. |
so, the saved instance state Bundle is for smallish data (forms, object identifiers, etc.) that you would like to hold onto for a bit while the user is off doing something else, but you won't mind if you never get it back
|
Steve S. |
that's a great explanation! very helpful
|
Steve S. |
My last question:
|
Steve S. |
View paste
|
Mark M. |
that's a typical pattern
|
Feb 21 | 7:45 PM |
Mark M. |
most of the time, with regular callbacks, you already know whether or not there is an activity
|
Mark M. |
(e.g., onDetach() there won't be; pretty much everything else, there will be)
|
Mark M. |
a background thread can't make that assumption
|
Steve S. |
makes sense
|
Steve S. |
no more Android questions today.
|
Mark M. |
OK
|
Steve S. |
i did want to ask if you mind if i post some of the content of your answers on Stack Overflow. (for instance, there's a question about the difference between onViewCreated/onCreateView)
|
Mark M. |
if you don't mind linking back to the chat transcript, go right ahead
|
Steve S. |
cool.
|
Steve S. |
thank you so much for all the great help. i really appreciate it!
|
Mark M. |
you're welcome!
|
Steve S. |
have a great rest of the day!
|
Mark M. |
you too!
|
Steve S. | has left the room |
Feb 21 | 8:25 PM |
Mark M. | turned off guest access |