1

I have developed a habit of taking a shortcut in programming, and I am wondering what the consequences are:

In MainActivity() declare:

 public static Context xt;

In the MainActivity constructor of MainActivity

 xt = this;

In my Renderer constructor:

 readTextFile(MainActivity.xt, R.raw.vertexcode);

and the function readTextFile uses context to open resources

public static String readTextFileFromRawResource(final Context context,
         final int resourceId)
 {
     final InputStream inputStream = context.getResources().openRawResource(
             resourceId);
      . . .
Makketronix
  • 1,389
  • 1
  • 11
  • 31
  • By saving a `Context` in a static variable you are creating a memory leak. Very very bad... Somehow all other answers miss that completely... – Xaver Kapeller Jul 19 '14 at 16:47

5 Answers5

2

Your implementation is bad, for me the bes use static variables is using Singleton. Singleton Pattern

Now, remember if you are using reference object for the Context, probably some methods could be alter the variable, and others function can suffer the consequences.

Chefes
  • 1,892
  • 18
  • 17
1

It's "bad" mainly because there's no guarantee that your MainActivity constructor has been called. This can happen if your process is terminated and then re-created while another activity is above it, for example.

You can use a custom Application subclass and store a static reference to its Context there, which will be available no matter how your app starts. (The exception is that ContentProviders may be instantiated before your Application subclass.) You may want to expose it via a static method instead of a static field.

But it would be better design to simply pass the Context as a constructor parameter to Renderer, if at all possible.

Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118
1

I think will be bad if your Application will lose the values of the static variables when the OS requires more memory, I use the onResume() method to set the value of my static variable context.

 public class MyApplication extends Activity{
    @Override
      protected void onResume() {
        MyApplication.context = getApplicationContext();
        super.onResume(); 
    }        
  }
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • Thank you for the information. I didn't know static variables values will be lost! – Makketronix Jul 19 '14 at 16:43
  • yes, see this http://stackoverflow.com/questions/4797187/android-static-variable-null-on-low-memory "In your onResume() method you could query the static data to see if it is present and if not, load it back in again." – Jorgesys Jul 19 '14 at 16:45
1

Your implementation is bad. Check Android Developers Blog.

Avoiding memory leaks

http://android-developers.blogspot.jp/2009/01/avoiding-memory-leaks.html

In this case (keep Context in a static field), GC do not release Context Object until the application process is killed. This pattern is so-called memory leak and should be avoid.

Mitsuaki Ishimoto
  • 3,162
  • 2
  • 25
  • 32
1

Tavian Barnes gave you a tip use a custom Application subclass. Its my technique on android apps and working great. Activities and in-process services can use it. Application instance is a system automanaged singleton, all apps have either default or custom one. I use it for app-global things.

AndroidManifest.xml

   <?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="com.my.app"
       android:versionName="1.0" android:versionCode="3" >

       <application
          android:name=".MyApplication"
          ...
       </application>
   </manifest>

com.my.app.MyApplication

package com.my.app;
import android.app.Application;

public class MyApplication extends Application {
    @Override public void onCreate() {...}

    public void doSomething() {
        // getApplicationContext() getter is available here
    }
}

Use this getter (MyApplication)getApplication() in any activity or inprocess service class.

phatfingers
  • 9,770
  • 3
  • 30
  • 44
Whome
  • 10,181
  • 6
  • 53
  • 65