Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources.getSystem() vs getResources()

Tags:

android

I am new to Android and I am learning the SDK myself from resource available over the net.

I came across a situation now. I am trying the below code:

Type 1:getResources().getString(android.R.string.cancel);

Type 2: Resources.getSystem().getString(android.R.string.cancel);

Type 3: getString(android.R.string.cancel);

All of the above methods return the same value. So what are these methods, what are their use cases. What are the good practices on when to use which method. Please help me out.

like image 609
Kannan Ramaswamy Avatar asked Dec 26 '11 06:12

Kannan Ramaswamy


1 Answers

The difference is not only in what you get, but in WHERE can you use them.

The first and the third ones are using "context." invisibly. So, very often (in static members or out of activity members) you can't use them directly, unless you pass context or resource as a static variable or as a parameter into your scope. But the second one

Resources.getSystem().getString(android.R.string.cancel)

You can use ABSOLUTELY EVERYWHERE in your application, even in static constants declaration! But for system resources only

like image 74
Gangnus Avatar answered Sep 17 '22 12:09

Gangnus