• 7

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I'm approaching completion of my first true App..
Along my road (in the past couple years) I've seen many examples of code.
One thing is consistent: I always see the use of static context references such as:
getApplicationContext(), this.getActivity(), and many other such references..

But earlier today, after asking how to retrieve Context from a Method within a Receiver,
I was told simply "pass it the Context", ex: public void receiverMethodCall(Context context).

My Question is: If it's that simple, why do people frequently make repeated static context references inline within their code, over and over again, instead of simply passing the containing Method a Context to begin with, and referring to it using "context" when needed?

I Hope that I got your question meaning - you got your answer and told to pass on the context as a context typed variable - but do not forget that when you want to call your method and pass it a context, you will have to know what this context is and initialize it.

For example, if you want to make a Toast one of the method parameters is context and if you are using a lot of Toasts it would be better to have context reference and use it for every time you want to show a Toast(or use any other method that requires context.)rather than using this.getActivity() orgetApplicationContext().
By doing this you can prevent a duplicated code and you won't need to refer to the context in your code a lot of times, now for the question about "why do people frequently make repeated static context references inline within their code".

I can think of one case when it won't be a bad thing to make 2 context reference: let's say that you have 2 methods in the same activity that are using context - if those methods are used once then you can create context reference inside the method and let the garbage collector handle that reference.

I do have to say that I agree with you, I think that if its optional just pass context to your method and let it use it, its really simple solution.

  • 1
Reply Report
      • 2
    • I upvoted and accepted your answer.. Seems pretty solid. Thanks again for taking the time to clarify and give your thoughts.
      • 1
    • Thank you Tamir, it seems that you agree with me.. I just can't quite understand why people don't do this more often, or even always, when its possible.. Instead, they make repeated (and often overly complicated) calls to get the context. Can anybody else provide further explanation as to why this isn't done regularly?
    • PS. Must the context be passed in the original (calling) methods variables to do this, such as in onReceive, and if so, can you simply add a context variable to an Activity so it can be passed to it's methods just as easily?
      • 2
    • Yes, you can save Context variable and call it every time you will need a method with context - but if you are not using context very often it may be a better idea to not use a global variable and use it as a local variable. And for your first question - there are a lof or different programmers there and seems like some of them just want to solve the issue in the fastest way possible even if it will mean to use repeatedly calls to get the context reference.