0

I am noob in Android. I see there are many methods associated to get context either activity or application context. What context you should use in what scenario ??

One blog saying to use the context-application instead of a context-activity to avoid memory leaks. How here memory can be leaked if I use Activity context

Any idea ???

Mick
  • 1,179
  • 5
  • 14
  • 24
  • 1
    this [post](http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context) is very good for your asking) – xoxol_89 Apr 15 '14 at 05:37
  • possible duplicate of [Difference between Activity Context and Application Context](http://stackoverflow.com/questions/4128589/difference-between-activity-context-and-application-context) – Pankaj Kumar Apr 15 '14 at 05:38
  • http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context refer this – GvSharma Apr 15 '14 at 05:39
  • @PankajKumar http://stackoverflow.com/questions/4128589/difference-between-activity-context-and-application-context#comment6225673_4128799 See here one guy saying to use `getApplicationContext` other memory leak how here memory leak can be ?? – Mick Apr 15 '14 at 05:47
  • @Mick Read http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html blog carefully – Pankaj Kumar Apr 15 '14 at 05:52
  • @PankajKumar I have read edited my question – Mick Apr 15 '14 at 05:54
  • http://possiblemobile.com/2013/06/context/ – laalto Jan 20 '15 at 12:08

1 Answers1

1

You create one Method that returns Context(this). Parse Activity to Application as below (under Methods defined in MainActivity class):

public Context getContext() {
        return getApplicationContext();
 }
public Activity getActivity() {
        return this;
 }

and then use:

Application abc = ((Application)getActivity());
Gordon Mackie JoanMiro
  • 3,499
  • 3
  • 34
  • 42