Office Hours — Today, May 6

Thursday, April 30

May 6
7:25 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:35 PM
Lucy
has entered the room
Lucy
Hello, Mark!
Mark M.
hello Lucy!
how can I help you today?
Lucy
I searched StackOverflow and other places but I didn't find a way to do this...but do you know if its possible to retrieve all <string-array> names from strings.xml programmatically without having to know all of them? For example, "For each <string-array> in strings.xml get me the names, in a list"
Mark M.
you should be able to use reflection to find the array resources
however, all arrays are in the same R space (R.array.*)
so you would not be able to tell a string array apart from an int array, for example
7:40 PM
Lucy
Oh! I see. I'll look into it.
I am calling startActivityWithResult to start ActivityB from FragmentA which is in ActivityA. I'm calling getContext().startActivityWithResult on ActivityB and I want ActivityA to receive the result. I saw some different answers on S.O. on this topic. Most said do not use getContext() but it doesn't work if I don't use it.
Mark M.
I assume you mean getActivity(), not getContext()
Fragment does not have a getContext() method IIRC
and Context does not know how to call startActivityForResult(), only Activity does
the only way to literally do what you want would be to call getActivity().startActivityForResult() on the fragment
Lucy
Sorry, you are correct. I'm calling: getActivity().startActivityForResult().
Mark M.
that being said, I'm not a fan
fragments should not be starting activities
fragments should not be doing *anything* outside the views of the fragment itself
anything that transcends the fragment is the responsibility of the activity, not the fragment
so, have the fragment ask the activity to handle such-and-so event
7:45 PM
Mark M.
and, perhaps today, the right answer is for the activity to call startActivityForResult)
but in the future, you might substitute in something else (FragmentTransaction for an inline fragment, DialogFragment, etc.)
Lucy
I see! OK, I will add a call to the Interface used for Fragment<-->Activity communication.
Mark M.
yes, if you're using that sort of "contract pattern" already, I'd definitely go that route
BTW, with respect to your earlier question, you'd call R.array.class.getFields(), I think: https://stackoverflow.com/a/11519091/115145
Lucy
OK cool, I'll add a small method to my interface.
That's terrific info, thanks! I had not found that answer.
So, I do have a short series of Activities, all containing Fragments, and I pass extras to each one along the way. I considered whether I should do it all in one or two Activities. Is that what you mean above by using FragmentTransactions instead?
Mark M.
well, I was thinking more of phone-vs.-tablet scenarios
Lucy
For tablet devices, I'll use less Activities and will use the Item/Detail pattern.
OK, good, I will do that for phone vs. tablet.
Mark M.
for phones, there are two dominant patterns:
7:50 PM
Mark M.
1. the Google approach, in terms of their project templates, which says that if you have Fragments fA and fB, and you're showing one at a time on phones, that you have activities aA and aB, each showing one fragment
2. the "WTF Google?" approach, where you still have fA and fB, but only one activity, that runs a FragmentTransaction to replace fA with fB (or vice-versa) as needed
in my experiments, in most cases, they are a wash in terms of complexity
where #2 wins is if using extras is a problem (your data cannot be put in an extra due to data type, or your data is too big)
Lucy
This is excellent information. I wondered if starting more Activities would be less performant just because they're Activities.
So far I'm able to use extras and the data passed is small (a couple of String or Date)
Is it OK to explicitly call finish() in order to stop the current Activity so that the Result will then be sent back to startActivityWithResult? I saw this pattern on S.O. and its working for me. I just wondered if its conventional?
Mark M.
yes, that's conventional
as soon as the user makes their choice, you call setResult() and finish()
Lucy
So far I'm calling it from the Fragment using: getActivity().finish()
Mark M.
that's another thing that I would delegate to the activity
Lucy
It works but perhaps I should move this call to the Activity the same as the other one.
Mark M.
agreed
7:55 PM
Lucy
Super!
That was all my questions for today. As always, this is very helpful!
Mark M.
glad it is proving useful for you
Lucy
has left the room
8:30 PM
Mark M.
turned off guest access

Thursday, April 30

 

Office Hours

People in this transcript

  • Lucy
  • Mark Murphy