How to get the context - android

I need to get the context to be able to get a resource. Like this:
getApplicationContext().getResources().openRawResource( R.raw.texture );
I've seen the getApplicationContext() in the android documentation but when I try to use it in the above code it doesn't work - it doesn't exist.
I can send the context through functions to get it to where it's needed and it works. However, I find it cumbersome to send a variable through many functions that doesn't need or use it. Then I would rather just try to get it in the function that do. But the getApplicationContext(), as in the android documentation, doesn't work - http://developer.android.com/reference/android/content/Context.html
So how do I get the context so I can read resources? Or are my only option to send it through all my functions?

getApplicationContext() is a method of a Context. You have to have a context to get the resources. That's just how it works.
Just makes sure you're not storing a reference to your context anywhere or you could cause a memory leak.

In your activity you can use getResources() method immediately. For example
getResources().getDrawable(R.drawable.logo);
If you want to get some resource in some other class not in activity, you should pass context link in other class from your activity. For example
Util.convertLogo(this)
or
Util.convertLogo(getApplicationContext())

Related

Where can I learn more of either pass in a Context (so you can access resources), or make the helper methods static?

I need to use Toast.makeText(getApplicationContext()). The getApplicationContext() is what I am really after. I need a specific tutorial on passing Context around. Any sugestions? I see alot of developers answering questions with techo-speak like this and I would seriously like to advance to that stage. I have run into passing Context issues way too often and would like a deep understanding of this. Until I get this type of knowledge I will always consider myself a noob.
Thanks.
Do you have a problem with Activity being a Context ? (Activity.this)??
Android already does that for you so why do you want to bother your head.? When you create a View the Context is passed to the View eg; TextView textv = new TextView(Context); you can later retrieve that Context with View.getContext(). Honestly Context are everywhere so why do you really want to use the getApplicationContext(); why not getBaseContext()

Best way to use getString()

I'm having some problems on where and how to place and call the getString().
I tried at the beginning of my activity where I usually define it but always get an error. Should it be only after onCreate()?
Some of my strings I use it very often so I don't want to use getString() every time I use them.
Is this the correct way?
private String helloWorld = getString(R.string.hello_world);
Thank you very much! :)
at the top of your file do:
private String helloWorld;
And then, in onCreate(),
do:
helloWorld = getString(R.String.hello_world).
The problem is that getString needs a reference to the activity context, which has not been associated with your class until onCreate is called.

In monodroid enviroment how do I get the Context outside of the View

I want to load an asset and the only way I can see to do that is with
Context.Assets.Open();
I don't see a way to get access to Context without passing it around. I really don't want to pass it around
To answer my own question:
Application.Context.Assests.Open()
is what I was looking for. For some reason I just couldn't see it.

Why do filestreams need a context?

I've been working on developing a library for my company for the past couple months and have been annoyed by the fact that filestreams seem to need a context whenever I store or load data to the internal storage.
I have designed the library to work like this :
A singleton class is made for providing and handling a keychain(containing app key and device id) and authinfo(user and password)
Whenever a request to call to a web service is made the calling class will attempt to get an instance of the singleton class and get the keychain and auth info through it like such :
SingletonClass.getInstance().getCredentials(Context ctx);
The result of this is that I need to constantly provide the context of the calling activity as most of my library revolves around calling an API with credentials and device id as parameters.
I am specifically referring to these lines :
....
FileOutputStream fos = ctx.openFileOutput(filename,
Context.MODE_PRIVATE);
fos.write(buf);
fos.close();
....
I am confused as to why the specific context of the activity calling is needed. Any help is greatly appreciated. Is there another way of solving this design issue ?
The application context should work for this - you could consider using this method: Using Application context everywhere? so you can easily get the application context without having to pass it around.
The file storage API is provided by the Context class.
So you need it.

Assets and reference

I want to know if is there a way to obtain a reference of an activity, from an other class, without passing it as parameter.
In particular, to open an Asset I need that the activity invoke getAssets(), but I haven't any reference to the activity.
The appropriate way is to pass the reference. You can store it as a static variable somewhere, but this is a very unrecommended method. If you don't have a reference, consider changing your design.
I'm not saying that only as an educative / theoretic argument only, but from (bad bad bad) experience.

Categories

Resources