-2

I've read What is context on android but still the following question is not clear for me:
Whenever I encounter the "context" parameter in any constructor, I pass the "this" keyword automatically and without thinking. Is this always the right approach?

  1. If yes: it means that "this" keyword is the unique answer to the "context" requirement. So what is this useless asking?
  2. If no: in which cases and what other objects maybe passed when a constructor asks for "context"?
    Excuse me for my stupid question if it's so. I'm a beginner to android programming and trying to understand the basic concepts.
Community
  • 1
  • 1
  • if context is required in Fragments, use `getContext()` and if your class is extending any Activity then you can pass `this`. – Akshay Bhat 'AB' Dec 09 '16 at 09:03
  • http://stackoverflow.com/questions/2914525/passing-activity-context-to-constructors-to-use-internally-is-this-bad – Ashish John Dec 09 '16 at 09:04
  • @abbath I've read [link](http://stackoverflow.com/questions/3572463/what-is-context-on-android) but i didn't find my answer. I want to now if i can **always** or **sometimes** use **this** keyword? – Behzad Hakimian Dec 09 '16 at 09:09

3 Answers3

1

it means that "this" keyword is the unique answer to the "context" requirement. So what is this useless asking?

this refers to this object. It works in your case because your subclassing from Activity of Service which inherit from ContextWrapper

If no: in which cases and what other objects maybe passed when a constructor asks for "context"?

Fragment is an example - but it has the method getActivity() to retrieve the hosting activity

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

NO

this keyword represent the current class.

Context is the android component, you can not find it in JAVA core. this keyword is the Core part of JAVA it means both 'this and Context' are different things. actually if you are extending any sub-class of Context like Activity, AppCompactActivity or FragmentActivity etc. you can use 'this' keyword to assign for Context but not everywhere.

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
  • It represents the current instance of the current class! – Jonas Köritz Dec 09 '16 at 09:12
  • Right, 'this' represent the current class instance. If you are extending any sub-class of Context or Context itself than you can use 'this' keyword inside that class for assign it to Context. – Vishal Chhodwani Dec 09 '16 at 09:17
  • why android is asking for context? doesn't it recognize on which context the method is running? – Behzad Hakimian Dec 09 '16 at 18:52
  • 1
    @BehzadHakimian: For this question I will explain with 2 Cases: **Case 1**: _If you are writing Context related code in Sub-class of Context._ Then there is no need to use Context it will detect automatically **Case 2**: _If you are writing Context related code in Non Sub-class of Context._ Then you have to use Context, it will unable to detect itself. And the important thing to note that, Context is used mainly when you require resources of your app or you doing any UI work. If you want to display some UI in Activity from different class then you need that Activity's Context. – Vishal Chhodwani Dec 10 '16 at 05:13
0

Assuming, you are passing this from an Activity class or some Component which inherits Context. There is one scenario where you do not have to pass this and instead of it, you have to pass getApplicationContext().

  • If you have a Singleton class in your project and you want to pass context to it. getApplicationContext() is recommended instead of this or activityContext. Because, activity reference should be available in memory till its lifecycle. On passing activity context in singleton class could cause memory leak.
Vinodh
  • 1,069
  • 2
  • 10
  • 22