getActivity()


#1

From the file CrimeListFragment:

[code]public class CrimeListFragment extends ListFragment {
private ArrayList mCrimes;
private static final String TAG = “CrimeListFragment”;

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	getActivity().setTitle(R.string.crimes_title);
	mCrimes = CrimeLab.get(getActivity()).getCrimes();
	
	CrimeAdapter adapter = new CrimeAdapter(mCrimes);
	setListAdapter(adapter);
}[/code]

I am a little confused about the getActivity() method in the line, getActivity().setTitle(R.string.crimes_title). I assume that it is setting the title of the Activity to “Crimes”, is that correct? What class is this method defined in? I have been having trouble finding the documentation for it. Can someone please explain this a little further and where the docs are?

And I am also confused about the line mCrimes = CrimeLab.get(getActivity()).getCrimes(). Can’t someone help here as well?

Thanks in advance.


#2

Can anyone help with this?

Thanks!


#3

Hi,

getActivity is a convenience method for the Fragment class and returns the Activity that the Fragment is currently associated with.

Here’s a link to the API doc for the Fragment class: Fragment Class API Doc