Friday, June 6, 2014

Save context in Android Application class

If you are tired of always passing application context through your classes in Android, you can try something like this. In this way you will store your application context in Application class as static field and you can access this field from anywhere.
public class App extends Application {

  private static Context context;

  @Override
  public void onCreate() {
   super.onCreate();
   context = this;

   /* other stuf */
  }

  public static Context getContext() {
   return context;
  }

}

No comments:

Post a Comment