Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to pass Context to non-activity classes?

So, my first major application is almost coded and I'm doing optimizations on my code. The app works fine, but I'm not sure about my way of passing the context to other classes. I don't want to do it the wrong way. I stumbled upon articles and questions here in Stackoverflow about contexts and which is the right way to pass it to non-activity classes. I read the documentation as well, but being a Finn makes complicated tech speak even harder to understand.

So, a simple question. Is my way of passing my main activity's context to other (helper) classes correct? If not, where can I read more about better practice on these situations.

For example: MainActivity.java

public class MainActivity extends Activity {     @Override     protected void onCreate(Bundle sis){         super(sis);         new Helper(MyActivity.this).makeMyAppAwesome();     } } 

Helper.java

public class Helper {     Context context;     Helper(Context ctx){         this.context = ctx;     }      public void makeMyAppAwesome(){         makeBaconAndEggsWithMeltedCheese(context);     } } 

Is this OK? It would be nice if someone could provide an easy to read article with examples on this subject.

like image 330
Iiro Krankka Avatar asked Oct 24 '11 19:10

Iiro Krankka


People also ask

What is the best way to pass context to a method?

Well, an even better way is to pass context directly to the method if using only few from a class for avoiding memory leaks. Dh. Yaduvanshi public class MainActivity extends AppCompatActivity { public static MainActivity mMainActivity; @Override private onCreate (Bundle savedInstanceState) { //... mMainActivity = this; } }

Should I need context in a member variable or not?

See the answer on here detailing that approach. It is usually in your best interest to just pass the current context at the moment it is needed. Storing it in a member variable will likely lead to leaked memory, and start causing issues as you build out more Activities and Services in your app. public void iNeedContext (Context context) {...

Why is my helper-class's context not being garbage-collected?

What he means is that your Helper-class might live longer then your MainActivity. Which means that there is still a reference to it (= it's context), so it can't be garbage-collected. And you have a memory leak. The best way to avoid this is: Don't store the context. Use an argument instead. Like `new Helper ().makeMyAppAwesome (ctx);.

How do you pass the current context as a parameter?

@Madhan Just pass the context through when it is needed. Never retain it. Any function that needs should accept it as a parameter. See the answer on here detailing that approach. It is usually in your best interest to just pass the current context at the moment it is needed.


1 Answers

You can do that using ContextWrapper, as described here.

For example:

public class MyContextWrapper extends ContextWrapper {      public MyContextWrapper(Context base) {       super(base);    }      public void makeMyAppAwesome(){         makeBaconAndEggsWithMeltedCheese(this);     } } 

And call the non activity class like this from an Activity

new MyContextWrapper(this); 
like image 59
ET-CS Avatar answered Sep 28 '22 06:09

ET-CS