-4

I have an idea they both get the 'context' so that functions know how they fit into the scheme of things, is this right?

Any clues for the evolving ape?

Sam
  • 1,659
  • 4
  • 23
  • 42
  • http://stackoverflow.com/questions/8215308/using-context-in-fragment/8215398?s=1|1.9530#8215398 – codeMagic Feb 10 '15 at 21:27
  • 3
    If you need to ask this question, you really need to spend more time learning the language you're writing and less time copy pasting from tutorials. – Gabe Sechan Feb 10 '15 at 21:34
  • Why is this question so heavily down voted? I gave you an up vote because your question led me to a satisfying answer. – John Doe Jun 19 '17 at 23:40

1 Answers1

7

this is a reference to the current class you are. It can be an Activity, Fragment, Adapter, View, etc. And when you use it what you are doing is just passing a reference of the current object of that class.

Let's say you are working on a custom View. Anywhere in the code of that view where you call this will be interpreted as the View itself, so the value of this changes depending on what class you are.

The method getActivity() is defined only in the Fragment class and any other class extending Fragment and this method returns and Object of the type Activity.

On Android development is very common mixing those two because most of the applications code is in Activity classes, and calling this on an Activity class will return an Activity object but as you can see they are not the same thing.

You can read more here

Carlos J
  • 2,965
  • 4
  • 17
  • 28