Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Application using fragments, plus a couple of resource.java files in the application

 
Glenn Hollowell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello-
I am working on an application that uses fragments, in addition I would like to have one or more java class files that the fragments could access as needed. This is done primarily of the sake of organization. The fragment that will be accessing the java class files looks like this:



The additional file, (SpeciesLookupEdit.java) that I want my fragment file to access looks like this:



While I could EASILY put saveRecord() in Fragment1.java, I would prefer that it go in it's own file for the sake of organization.
This seems incredibly easy, (...embarassingly) but I have not been able to get it to "go" with LogCat showing problems cascading from the onClick event in Fragment1.java.

QUESTION: In SpeciesLookupEdit.java, should it be,
"public class SpeciesLookupEdit extends Tab3Fragment{", or
"public class SpeciesLookupEdit extends Activity {", or just
"public class SpeciesLookupEdit{" ?

Thanks in advance for shedding light and sharing wisdom on this.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's a fragment, then it should extend Fragment. If it's an activity, then it should extend Activity. It sounds as if it would be neither, though, so it shouldn't extend either. If it needs a reference to an Activity or an Application (like for the logging call), then you can pass that in via the constructor.
 
Glenn Hollowell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Ulf.
Is this what you are suggesting?

That helped. Basically I am looking to create a repository for methods to keep them organized.
Other suggestions?
What is the appropriate Activity to get a toast to show in the above instance?
Thanks again.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be the activity that's currently being shown - which is the only one guaranteed to exist, after all.

I don't see how getActivity() is supposed to work in that code, though - you need to pass
the activity in via a constructor.
 
Glenn Hollowell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply Ulf. It did seem to me that logically this should (and would) be an extension of the java class Tab3Fragment.java with the methods in SpeciesLookupEdit.java functioning as if they were in Tab3Fragment.java. Suggestions?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as I said earlier, if it's not a fragment then it shouldn't extend Fragment or any class that extends it. That would be worse than having it all in one class. Plus, it wouldn't even work, because getActivity doesn't return anything useful in a class that isn't actually used as a fragment.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic