Makketronix
Makketronix

Reputation: 1460

Is refering to static Context in MainActivity from other classes bad?

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);
      . . .

Upvotes: 1

Views: 1187

Answers (5)

Whome
Whome

Reputation: 10400

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.

Upvotes: 1

Mitsuaki Ishimoto
Mitsuaki Ishimoto

Reputation: 3181

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.

Upvotes: 1

Jorgesys
Jorgesys

Reputation: 126455

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(); 
    }        
  }

Upvotes: 1

Tavian Barnes
Tavian Barnes

Reputation: 12922

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.

Upvotes: 1

Chefes
Chefes

Reputation: 1942

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.

Upvotes: 2

Related Questions