May 6 | 7:25 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
May 6 | 7:35 PM |
Lucy | has entered the room |
Lucy |
Hello, Mark!
|
Mark M. |
hello Lucy!
|
Mark M. |
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
|
Mark M. |
however, all arrays are in the same R space (R.array.*)
|
Mark M. |
so you would not be able to tell a string array apart from an int array, for example
|
May 6 | 7:40 PM |
Lucy |
Oh! I see. I'll look into it.
|
Lucy |
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()
|
Mark M. |
Fragment does not have a getContext() method IIRC
|
Mark M. |
and Context does not know how to call startActivityForResult(), only Activity does
|
Mark M. |
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
|
Mark M. |
fragments should not be starting activities
|
Mark M. |
fragments should not be doing *anything* outside the views of the fragment itself
|
Mark M. |
anything that transcends the fragment is the responsibility of the activity, not the fragment
|
Mark M. |
so, have the fragment ask the activity to handle such-and-so event
|
May 6 | 7:45 PM |
Mark M. |
and, perhaps today, the right answer is for the activity to call startActivityForResult)
|
Mark M. |
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
|
Mark M. |
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.
|
Lucy |
That's terrific info, thanks! I had not found that answer.
|
Lucy |
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.
|
Lucy |
OK, good, I will do that for phone vs. tablet.
|
Mark M. |
for phones, there are two dominant patterns:
|
May 6 | 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
|
Mark M. |
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
|
Mark M. |
in my experiments, in most cases, they are a wash in terms of complexity
|
Mark M. |
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.
|
Lucy |
So far I'm able to use extras and the data passed is small (a couple of String or Date)
|
Lucy |
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
|
Mark M. |
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
|
May 6 | 7:55 PM |
Lucy |
Super!
|
Lucy |
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 |
May 6 | 8:30 PM |
Mark M. | turned off guest access |