Resources.getSystem() vs getResources()

后端 未结 3 2071
死守一世寂寞
死守一世寂寞 2021-02-07 07:10

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:

相关标签:
3条回答
  • 2021-02-07 07:22

    These three methods are all the same. Here is the root method : getApplicationContext().getResource().getString("") and here is shortcut method : getString("") Use the shortcut method when you are processing on apps context. In almost every case, we use getString() to get Strings defined in the String file.

    0 讨论(0)
  • 2021-02-07 07:26

    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

    0 讨论(0)
  • 2021-02-07 07:45

    All 3 return the same value, but Resources.getSystem() references to the system resources and might cause a crash if used incorrectly.

    The advised usage is "getString(android.R.string.cancel);"

    It is also used as such in the WalkieTalkieActivity.java code on the Android developer website.

    0 讨论(0)
提交回复
热议问题