Aug 27 | 7:20 PM |
Mark M. | has entered the room |
Aug 27 | 7:25 PM |
Mark M. | turned on guest access |
Jeff D. | has entered the room |
Mark M. |
hello, Jeff!
|
Mark M. |
how can I help you today?
|
Jeff D. |
Hi Mike.
|
Jeff D. |
I'm going through my own View Pager right now.
Its hosted by a Fragment Activity, and has a FragmentPagerAdapter within
the pager.
|
Gaurav B. | has entered the room |
Jeff D. |
I'm having an issue with config changes, where the old fragments do not reattach to the activity within the pager
|
Mark M. |
(BTW, hello, Guarav -- be with you in a bit!)
|
Aug 27 | 7:30 PM |
Mark M. |
Jeff: usually, that just works, as FragmentPagerAdapter sets the tags on the fragments
|
Jeff D. |
I'm setting setRetainInstance(true) on the fragments that the FragmentPagerAdapter inflates
|
Jeff D. |
I don't have tags set in the .xml files....
|
Mark M. |
again, FragmentPagerAdapter defines the tags
|
Jeff D. |
i would think it would work as well...i'm just at a loss on why this doesn't work.
|
Mark M. |
it's part of the stock implementation
|
Mark M. |
what is the specific behavior that you are seeing, when there is a configuration change?
|
Jeff D. |
Its nothing more than a countdown timer for a crossfit application im working on.
|
Jeff D. |
just during the phone rotation. people are
wanting me to go into the manfest and do the whole
'configchanges|orientation, but i know that is a bad idea
|
Mark M. |
again, I need to know the specific symptoms that you are seeing
|
Mark M. |
for example, are you crashing?
|
Jeff D. |
No...no crashing. whenever the phone rotates, it goes into a state like the application just started up fresh
|
Jeff D. |
if i'm counting down, or have a time set, it looses it, and looks like a fresh application start
|
Jeff D. |
the fragments do not reattach.
|
Jeff D. |
I have my code up on github.
|
Mark M. |
um, well, bear in mind that retaining a fragment does *not* retain its views
|
Jeff D. |
so if i have an edit text and i rotate the phone, it won't replace it?
|
Mark M. |
on the contrary, the configuration change *will* replace the EditText
|
Aug 27 | 7:35 PM |
Mark M. |
and, for an EditText, the built-in onSaveInstanceState() logic will hold onto the entered text
|
Jeff D. |
by virtue of calling the super class?
|
Mark M. |
or just not overriding it in the first place -- either way
|
Jeff D. |
no kidding...hmm...
|
Mark M. |
for example, in the book, where I cover
configuration changes, I have the sample app where you let the user pick
and view a contact
|
Mark M. |
the Button for viewing a contact will be enabled only after the user picks a contact (otherwise, there is nothing to view)
|
Mark M. |
however, even though I retain the fragment, I
still need to call setEnabled() on the Button, as "being enabled or not"
is not automatically managed by onSaveInstanceState()
|
Mark M. |
retaining the fragment retains the *contact*,
though, so I can conditionally enable the Button in onCreateView(),
depending upon whether or not the user has picked a contact
|
Jeff D. |
so I'm assuming that restoring the fragments does too much for me..
|
Jeff D. |
your guide has a countdown timer in it...if i was
to roate the phone mid way through, would your application go back to a
fresh state? I would have to restart the runnable?
|
Mark M. |
off the top of my head, I do not remember, sorry
|
Jeff D. |
no issues, you have a ton of stuff in your book...impossible to remember everything.
|
Jeff D. |
a more general question then...would I have to restart any runnables that are called before the config change?
|
Mark M. |
I don't know what "restart any runnables" means in this context
|
Mark M. |
let me take a question from Guarav, and I'll be back with you in a bit
|
Mark M. |
Guarav: do you have a question?
|
Aug 27 | 7:40 PM |
Mark M. |
um, OK, back to Jeff, then
|
Jeff D. |
I have a Fragment within the Adapter that calls
Runnable that is being used for my countdown timer, a postDelayed call
comes in every so often to update the time.....if this runnable is not
retained by the OS automatically...do i have to restart it?
|
Jeff D. |
whenver a config change happens?
|
Mark M. |
where are you stopping the postDelayed() loop?
|
Jeff D. |
when the counter reaches 0.
|
Mark M. |
if you are doing that in onDestroy(), you will need to start up a fresh one in the new activity instance
|
Jeff D. |
I'm not stopping it at all...not calling OnDestroy or anything...I had assumed the OS would reattach EVERYTHING....
|
Jeff D. |
bad assumption :)
|
Mark M. |
I don't think I have tried your scenario
specifically, but it should be easy enough for you to test, via log
messages in your Runnable's run() method
|
Aug 27 | 7:45 PM |
Jeff D. |
understood. would the TextView content be saved
upon screen rotation? doing the manifest trick (config|orientation) IS a
bad idea, correct?
|
Mark M. |
I would expect text in a TextView to be saved,
though usually that's static and set in the layout file, so I don't know
when I have last tried a configuration change on dynamic TextView text
|
Mark M. |
using android:configChanges should be a last
resort, for cases where it's flat-out impossible to hold onto state any
better way (e.g., game trying to rebuild a GLContext)
|
Ollie | has entered the room |
Jeff D. |
I tried to save items in the onSaveInstanceState
within the Fragment, and pull them back out in OnCreate...but the Bundle
is empty for some reason...I am able to save items in a Bundle at the
Activity level, but its like the items that I save in the Fragment never
reach the Activity for saving...
|
Mark M. |
I have an example demonstrating that working, in the same chapter where I cover the rest of the configuration changes
|
Mark M. |
though I don't have a mash-up of using both a retained fragment *and* custom onSaveInstanceState() logic
|
Mark M. |
let me swing back to the others, and I'll return to you in a bit
|
Jeff D. |
no problems
|
Mark M. |
hi, Ollie: do you have a question?
|
Ollie |
Nothing right this sec
|
Mark M. |
OK
|
Mark M. |
if you think of something, let me know
|
Mark M. |
Guarav: do you have a question?
|
Aug 27 | 7:50 PM |
Mark M. |
OK, Jeff, back to you
|
Jeff D. |
appreciate the help. So if i save something at
the Fragment level (contents of a Text View, values that give the
current state), and I pass that to the super class...it should still be
saved, correct? I save items at the Fragment level and never get them
back in the OnCreateView in the Fragment...I can both save and extract
items at the Activity level....
|
Mark M. |
yes, that should work
|
Mark M. |
I can't rule odd oddities based on you both retaining the fragment and trying to use onSaveInstanceState(), though
|
Jeff D. |
Hmmm...i guess I broke something alone the way... maybe missing a call somewhere.
|
Jeff D. |
ohhh...should I be using one and not the other?
|
Mark M. |
in principle, I would not expect problems using both
|
Mark M. |
in practice, I suspect that most developers that retain the fragment do not bother with onSaveInstanceState()
|
Aug 27 | 7:55 PM |
Mark M. |
holding onto the "state" in data members of the fragment instead
|
Mark M. |
why are you retaining the fragment?
|
Jeff D. |
I figured it would retain EVERYTHING...the Text
Views, the state of visibily of certain ImageButtons, and the Runnables I
have to count.
|
Mark M. |
no
|
Jeff D. |
I'm learning that :)
|
Mark M. |
*all* setRetainInstance() does is keep the Fragment instance itself around
|
Mark M. |
and hence, anything it directly holds onto
|
Mark M. |
it specifically does *nothing* with respect to
retaining anything involving the UI, unless you do that yourself (e.g.,
my Button-and-contact scenario)
|
Jeff D. |
well....nuts.
|
Mark M. |
unless you have a clear reason to retain the fragment (e.g., it's managing an AsyncTask), I'd skip retaining the fragment
|
Jeff D. |
if it retains the instance of something complex like an AsyncTask...why not the current values of a TextView?
|
Mark M. |
you are comparing apples and apple crates
|
Mark M. |
complexity is not the issu
|
Mark M. |
er, issue
|
Jeff D. |
Is the Runnable, postDelayed that is currently running a reason to retain the fragment?
|
Mark M. |
retaining a fragment does not *directly* have anything to do with managing an AsyncTask
|
Aug 27 | 8:00 PM |
Mark M. |
however, *if* you have a retained fragment hold
onto the AsyncTask, then the AsyncTask can safely interact with the
fragment during a configuration change
|
Mark M. |
because the fragment isn't being destroyed/recreated
|
Jeff D. |
gotcha
|
Mark M. |
I would not consider a postDelayed() loop to be a reason to retain a fragment
|
Jeff D. |
understood.
|
Mark M. |
(BTW, Guarav and Ollie -- if you have a question, chime in)
|
Jeff D. |
Yes, I'm monopolizing right now, but will step aside if you guys have anything.
|
Jeff D. |
Ok, I'll take a look at this tonight and redo that
side of the application, in addition to why the Fragments Bundle
onsavestate is not reaching the Activity.
|
Aug 27 | 8:05 PM |
Jeff D. |
Mark, thanks for your time.
|
Mark M. |
you are very welcome
|
Ollie |
I am just here to learn all I can on fragments. Still haven't wrapped my head around them yet.
|
Mark M. |
I take it the book chapter on them didn't help?
|
Aug 27 | 8:10 PM |
Ollie |
It did, but for some reason I just am having issues thinking sometimes lol
|
Mark M. |
OK
|
Ollie |
It will click eventually I am sure
|
Aug 27 | 8:30 PM |
Mark M. |
well, that's a wrap for today's chat
|
Mark M. |
the transcript will be posted to http://commonsware.com/office-hours/ shortly
|
Ollie |
ok man, have a good evening
|
Mark M. |
the next chat is Thursday, 7:30pm Eastern
|
Mark M. |
have a pleasant day!
|
Jeff D. | has left the room |
Gaurav B. | has left the room |
Ollie | has left the room |
Mark M. | turned off guest access |