Mark M. | has entered the room |
Mark M. | turned on guest access |
Jun 9 | 7:25 PM |
Bryan | has entered the room |
Mark M. |
hello, Bryan!
|
Mark M. |
how can I help you today?
|
Bryan |
Hey Mark, how are you?
|
Bryan |
I was in last week asking about an issue I had where I was getting IllegalStateException Fragment already added. I was able to find out what was causing the issue
|
Bryan |
but not sure how to fix it
|
Bryan |
or rather, not sure how to fix it like I would like to
|
Mark M. |
yeah, that's sometimes a problem...
|
Yogesh | has entered the room |
Bryan |
essentially, what happens is, due to a the ability to click on multiple events at once, I can get to a place where I am on Fragment A
|
Bryan |
and the back button takes me to fragment A
|
Bryan |
and I get the exception
|
Mark M. |
(BTW, hello, Yogesh -- I will be with you shortly!)
|
Yogesh |
hello mark
|
Mark M. |
Bryan: um, so you're showing A, and BACK is supposed to take you back to A?
|
Bryan |
I can fix it by just doing a try catch for the IllegalStateException, but i would rather fix it by being able to know where I am going "back" to and comparing it to where I am
|
Bryan |
no, it shouldn't, but because of the messed up navigation I end up in that state
|
Mark M. |
ah, OK
|
Jun 9 | 7:30 PM |
Bryan |
If I have Frag B on the backstack, and when that get's undone, it goes back to Frag A, is there a way to get that info?
|
Bryan |
I am not sure if I am asking that right
|
Mark M. |
so, you're showing B, and you want to know whether or not if the user presses BACK, you would wind up at A?
|
Bryan |
no, I want to know what I am going to go to if the user presses back
|
Bryan |
whether B, C, eetc
|
Bryan |
etc
|
Mark M. |
not really
|
Mark M. |
unless you track that yourself
|
Bryan |
Ok, so even though Android knows the transaction it will go back to, there is no easy way to pull that out of accessible information?
|
Bryan |
unless I track it
|
Mark M. |
or try using the string passed into addToBackStack() to track it
|
Bryan |
Well, in this case A was never added to the back stack
|
Bryan |
B was the first thing added, but obviously Android knows that B goes back to A even though I didn't add A to the backstack
|
Mark M. |
then Android does not know that B goes back to A
|
Mark M. |
the only "goes back to" that Android applies at the fragment level automatically is via adding to the back stack
|
Mark M. |
anything else you are doing yourself, or you are transitioning an activity boundary (and so it's the overall task's back stack that you're seeing)
|
Bryan |
the definition says that addobackstack adds the current transaction
|
Bryan |
and popping that "undoes" the transacgtion
|
Mark M. |
right
|
Bryan |
it doesn't speak to what shows up when it is "undone"
|
Mark M. |
well, if there's any question about it, you've got other issues
|
Bryan |
that is true
|
Mark M. |
since you're using capital letters, let me use a different naming scheme
|
Bryan |
please do
|
Mark M. |
you start off with F1 and F2 in the FragmentManager, both visible
|
Mark M. |
you create a FragmentTransaction to replace() F2 with F3, and use addToBackStack() on that transaction
|
Jun 9 | 7:35 PM |
Mark M. |
you commit() the transaction, and you wind up with F1 and F3
|
Yogesh | has left the room |
Mark M. |
as being the visible fragments
|
Mark M. |
user now presses BACK, or you pop the back stack via FragmentManager
|
Mark M. |
"undoes" means "undo the replace()"
|
Bryan |
yes
|
Mark M. |
so F3 is removed and F2 is put back
|
Mark M. |
and so you're back to F1 and F2
|
Bryan |
right
|
Bryan |
but if I didn't track that myself, there is no way to know that information from the FragmentManager, right?
|
Mark M. |
well, addToBackStack() takes a String "name" parameter
|
Bryan |
but F1 and F2 are not in the backstack, right?
|
Bryan |
in your example
|
Mark M. |
principally, that's used to pop several entries off the stack, to jump back to the named state
|
Mark M. |
correct
|
Mark M. |
well, it depends on what you mean by "in the backstack"
|
Mark M. |
the stack itself is transactions, not fragments
|
Mark M. |
depending on nomenclature, one could argue that F2 is on the back stack
|
Mark M. |
insofar as pressing back returns you to F2
|
Mark M. |
anyway, back to the name
|
Bryan |
yes
|
Mark M. |
I haven't used the name, as I haven't had a case where I needed it
|
Mark M. |
if you're not looking to use the name for anything else, you could use the name as a string sort of "tag" to help you identify what the transaction was
|
Mark M. |
such as "F2->F3"
|
Mark M. |
or whatever
|
Mark M. |
FragmentManager lets you iterate over the back stack entries, and one thing that you can get is that name
|
Jun 9 | 7:40 PM |
Bryan |
so I can use that string to know
|
Mark M. |
however, details of what the actual transaction was that will be rolled back are not in those back stack entries
|
Bryan |
I see what you are getting at
|
Bryan |
since there is nobody else here yet, can I try to explain better how my app works, I would be curious to have you critique it anyway
|
Mark M. |
now, it's possible that this won't work, if they require uniqueness and you determine that you don't have unique strings
|
Bryan |
and it might show my problem more clearly
|
Mark M. |
yeah, hopefully Yogesh will return
|
Bryan |
lol
|
Bryan |
Ok, I am trying to write a single activity app that just uses replace for fragments
|
Bryan |
I have a nav drawer with home/activity/profile/contest/logout
|
Bryan |
when you launch the app, the activity loads a blank page that never gets shown, and it does fm.replace(R.id.Container, HomeFrag)
|
Bryan |
with no back stack
|
Bryan |
all of the nav drawers do the same calls, fm.replace(R.id.Container, HomeFrag/ActivityFrag/etc)
|
Bryan |
with no back stack
|
Mark M. |
OK
|
Bryan |
but any clicks inside (like clicking on a list entry in HomeFrag) call fm.replace(R.id.Container, DetailFrag)
|
Bryan |
WITH backstack
|
Mark M. |
OK
|
Bryan |
so what is happening is that for different reasons, people are able to click on the list to get to the DetailPostFrag (which then should go back to HomeFrag) AND at the same time get into the HomeFrag
|
Bryan |
i know one way they are doing it (by clicking on the menu at the same time as the post)
|
Bryan |
and then while they are in the HomeFrag, they press back, which takes them to HomeFrag firing off the exception
|
Jun 9 | 7:45 PM |
Bryan |
I have found a number of smaller fixes to take care of some individual things
|
Bryan |
but I was hoping that I could just say....If my back takes me to the same place I am, ignore
|
Bryan |
else, super.onBackPressed()
|
Mark M. |
you should be clearing the back stack on any trip back to HomeFrag
|
Mark M. |
any time you go to an item in your nav drawer, clear the back stack
|
Bryan |
I did that, but I was still getting the crash from live users
|
Bryan |
I was not able to duplicate it for myself
|
Bryan |
I use crashlytics, and it was still showing users getting that error even with the patched version (that cleared the backstack on menu item)
|
Bryan |
obviously can't expect you to fix that one since I don't have enough data, but.....
|
Mark M. |
I think last time, I suggested that you use dump() on FragmentManager
|
Bryan |
yes
|
Bryan |
you did
|
Mark M. |
but, that was thinking that you were able to reproduce the problem
|
Bryan |
and we were (one way) and we fixed it, but there were other ways people were getting into the same situation
|
Bryan |
unless for some reason, crashlytics was mis-reporting the version
|
Mark M. |
well, it's unlikely that they'll make up a version code :-)
|
Mark M. |
so presumably their info is correct
|
Mark M. |
short of a code review, to ensure that you are *only* using addToBackStack() on transactions that are drilling down into detail (and not going to a nav drawer entry), I don't really know what to tell you
|
Jun 9 | 7:50 PM |
Mark M. |
somehow, you are adding a HomeFrag where there's a back stack entry that would return the user to HomeFrag
|
Bryan |
yeah, this has been helpful regardless, I like using the transaction name to track where it came from, that might work
|
Bryan |
yes
|
Mark M. |
another thing that might help with diagnostics is to add a back stack change listener
|
Mark M. |
and in your debug builds, use it to dump what's currently in the back stack
|
Mark M. |
to see if it makes sense
|
Mark M. |
since you can no longer reproduce the problem, it won't help you directly here, but it may help suss out where you have other back stack problems
|
Bryan |
I can reproduce the problem at this point, because I undid my "fixes"
|
Mark M. |
and, perhaps, tugging on that thread will unravel the knot that is causing your crashes
|
Bryan |
ok, thanks Mark, I appreciate it
|
Bryan |
what are your thoughts on single activity apps?
|
Mark M. |
problems like this make them painful, akin to the equivalent sort of problem in single-page Web apps
|
Mark M. |
in theory, they're fine
|
Bryan |
:)
|
Bryan |
how long have you been programming android?
|
Bryan |
(java)
|
Mark M. |
February 2008
|
Mark M. |
(light poking at it before then, but nothing serious)
|
Bryan |
nice, well, thanks again, I really like what you are doing here, with the book, updating regularly and office hours
|
Bryan |
it is hugely helpful!
|
Mark M. |
I aim to be useful
|
Bryan |
what do you charge for code review?
|
Bryan |
or per hour/day or however you would do it
|
Jun 9 | 7:55 PM |
Mark M. |
actually, when I mentioned a code review, I wasn't implying that I'd be doing the review :-)
|
Mark M. |
I try to avoid getting into other firms' proprietary code, just because the legal agreements get icky
|
Bryan |
I know, but I saw that you do consulting like that, right?
|
Mark M. |
yes, but more on a Q&A/discussion sort of basis
|
Mark M. |
the sort of thing a garden-variety NDA can cover
|
Bryan |
gotcha
|
Bryan |
thank you!
|
Bryan |
have a great night, go cavs!
|
Mark M. |
lucky for you, I'm not in the Bay Area, like lots of Android devs :-)
|
Bryan |
lol :), it was worth the risk
|
Mark M. |
and the Sixers aren't going to be making the NBA Finals any time soon
|
Bryan |
But I grew up in the bay area, so I am covered either way
|
Bryan |
who knows, they have an interesting rebuilding scheme
|
Bryan |
maybe it will be like moneyball
|
Bryan |
adios sir, I am sure I will see you again soon
|
Mark M. |
have a pleasant day!
|
Jun 9 | 8:10 PM |
Bryan | has left the room |
Jun 9 | 8:30 PM |
Mark M. | turned off guest access |