So I've noticed that there seem to be two ways to get the same data, and I'm not sure if there are guidelines to when you should use either (other than, bypassing the getResources could be memory saving if you don't actually want to use the object more than once). But other than that I would like to know if there are guidelines or reasons to use
Context.getText(id) vs Context.getResources.getText(id)
Can anyone help?
The documentation for getResources() says that it will [r]eturn a Resources instance for your application's package. In code examples I've seen this used to access the resources in res , but it seems like you can just access them directly. For example to retrieve my_string from res/values/strings.
Context. MODE_PRIVATE is an int constant with value zero; refer to the javadoc linked above for the details. The former is more readable though, and that makes it preferable from a code style perspective.
Definition. it's the context of current state of the application/object. It lets newly-created objects understand what has been going on. Typically, you call it to get information regarding another part of your program (activity and package/application).
You can go for getApplicationContext() if you wanna get context of whole application. If you want to get context of current class you can use getBaseContext() instead.
There is no difference. The source for getText(id) is:
/**
* Return a localized, styled CharSequence from the application's package's
* default string table.
*
* @param resId Resource id for the CharSequence text
*/
public final CharSequence getText(int resId) {
return getResources().getText(resId);
}
You can see for yourself at Context.java on netmite which has a version of the Android source.
If you just want the text, you can use the Context.getText(id)
method. Getting the resource with Context.getResoures()
allows you to test other properties of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With