Office Hours — Today, August 18

Yesterday, August 17

Aug 18
3:50 PM
Mark M.
has entered the room
Mark M.
turned on guest access
3:55 PM
Emmanouil K.
has entered the room
Mark M.
hello, Emmanouil!
how can I help you today?
Emmanouil K.
hi Mark.
Need your knowledge about fragments
You see, I try to create a functionality, that I will be able to go forth and back between fragments. Obviously the backstack will not help, since I am aware that it just pops
Mark M.
use show()/hide() or attach()/detach()
(methods on FragmentTransaction)
Emmanouil K.
So let's say I have fragment A->B->C-D, and I am on fragment V, If I want to go forth and fragment C is already created, to be able to re-open it and resume it from the old state
Apologies, ment I am on Fragment B*
Mark M.
whew, I was getting worried there :-)
4:00 PM
Mark M.
have your Activity hold onto four Fragment objects in fields, for A, B, C, D
Emmanouil K.
I have already tried the show/hide but unfortunately, the fragments are not populated the way I wish, they dont have the same state
yes
Mark M.
the state should not be affected by show()/hide()
all that is happening is that the views are marked as GONE, as far as I know
Emmanouil K.
I see.
Mark M.
attach()/detach() will remove the views from the hierarchy, but Android holds onto the fragment's saved instance state Bundle
attach()/detach() therefore save more heap space, but attach() takes a bit more execution time, as onCreateView() should get called again
Emmanouil K.
I have also tried to call the fragments by the TAG (in case they are already created of course), but for some reason there are a few that they will not open with the old state. Do you think I am doing something wrong there?
Mark M.
I have no good way of answering that
you might add more logging to determine what is going on
Emmanouil K.
I see, was just looking for alternatives, but I will try the show and hide again as you suggested, perhaps I did something silly
Mark M.
IOW, are you somehow overwriting the state? or are you winding up with new fragment instances when you are not expecting?
4:05 PM
Emmanouil K.
the second.
Mark M.
findFragmentByTag() will not create a new instance, assuming that's what you mean by "call the fragments by the TAG"
Android creates new fragment instances on a configuration change
Emmanouil K.
yeah I am using the findFragmentByTag
Mark M.
otherwise, you're the one who creates the fragment instances, as part of your FragmentTransactions
so, if you are getting new fragment instances, and you have not undergone a configuration change, the new instance should have come from your code
you might create a do-nothing zero-argument public constructor, where you can put a breakpoint or logging or something
to try to see who is creating instances and when
Emmanouil K.
Do you have any Github sample I could use in your repo?
Mark M.
sample of what, specifically?
Emmanouil K.
fragment navigation
Mark M.
um, I have a bit in the large-screen chapter
Emmanouil K.
Good stuff
Mark M.
that would be the LargeScreen/ directory in the repo
Emmanouil K.
Thanks a lot
Mark M.
otherwise, I do not do much with fragment navigation in the book, sorry
4:10 PM
Emmanouil K.
no probs
Thank you for the advice. Have a good day. It was helpful.
Mark M.
you're welcome!
4:30 PM
Emmanouil K.
has left the room
5:00 PM
Mark M.
turned off guest access

Yesterday, August 17

 

Office Hours

People in this transcript

  • Emmanouil Kounalakis
  • Mark Murphy