1

I need to have have reference to Context in my utils class.

First I am extending Application class and initializing my util class:

public class MyApplication extends Application {

  @Override
  public void onCreate() {
      super.onCreate();
      Utils.init(getApplicationContext());
  }
}

And utils class looks like:

public class Utils{

    private static Context sContext;

    private Utils() {
    }

    public static void init(Context context) {
        sContext = context;
    }
}

Is there any possible way to get a leak with such approach?

I can see only one case: when application goes background - Context can be re-created, and so Utils class may be re-initialized even if it will persist in memory.

Any suggestions, please.

CAFEBABE
  • 3,983
  • 1
  • 19
  • 38
Alex Moore
  • 36
  • 4
  • Yes this can lead to memory leaks, when screen orientation is changed.. Can you tell me why you need to hold context as your Utils class's private field? – Bhargav Mar 05 '16 at 16:08
  • This was just theoretical, of course. But now, how orientation changes affect these classes? There is no any Activity references. – Alex Moore Mar 05 '16 at 16:11
  • I guess as long as you are passing in applicationContext to the init methods its fine, but in order to make sure that you are keeping only app context in your `Utils` class you need to do this in init method `sContext = context.getApplicationContext();` – Bhargav Mar 05 '16 at 16:12
  • And also instead of holding a static context like that why not make `Utils` class a singleton, and use Dependency Injection in whichever class depends on Utils class. With dagger2 you can actually give scope to injected classes like these, so instead of actually being a singleton for whole app life cycle, you can actually "scope" it to custom abstract scopes you want – Bhargav Mar 05 '16 at 16:15
  • That's interesting idea, thanks – Alex Moore Mar 05 '16 at 16:19
  • if you like I could explain it in more detail in an answer... – Bhargav Mar 05 '16 at 16:27
  • Visit http://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android?rq=1 – Rindt Lin Mar 05 '16 at 16:29
  • Like @Bhargav pointed out, use ApplicationContext whenever possible rather than AcitivityContext. ActivityContext is renewed everytime the screen orientation changes and keeping references to those is absolutely a bad idea. – Ashim Mar 05 '16 at 17:12
  • Sure, but in this case there is no any references to Activity – Alex Moore Mar 05 '16 at 18:04

1 Answers1

1

You should solve as follow:

 public class YourClass extends Application {

        private static Context context;

        public void onCreate()
        {
            super.onCreate();
            YourClass.context = getApplicationContext();
        }

        public static Context getAppContext() {
            return YourClass.context;
        }
    }

How to use:

YourClass.getAppContext();
user3449772
  • 749
  • 1
  • 14
  • 27
  • Yes, this is known approach. But question was bit deeper - is there leak possibility when referencing like there? – Alex Moore Mar 05 '16 at 16:08
  • I think that your solution should not be a problem because the static method is loaded and unloaded instantly and you should not have some memory leak issues. Anyway, it think your code is similar to my solution. – user3449772 Mar 05 '16 at 16:16
  • Sure, in a normal situation i'd use your approach.But not all situations go ways we want – Alex Moore Mar 05 '16 at 16:18