When to use ContextCompat class

后端 未结 3 2016
梦谈多话
梦谈多话 2021-02-07 02:00

I want to know when to use ContextCompact class in an application. Basically what is it used for and when to use it? I have read developers site, it says Cont

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-07 03:04

    ContextCompat is a class for replacing some work with base context.

    For example if you used before something like

    getContext().getColor(R.color.black);
    

    Now its deprecated since android 6.0 (API 22+) so you should use:

    getContext().getColor(R.color.black,theme);
    

    or use ContextCompat which fill theme automatically depends on your Context's theme:

    ContextCompat.getColor(getContext(),R.color.black)
    

    Same thing with getDrawable

    Also ContextCompat contains other methods for functional of API 22+ such as checking permissions or adding multiple activity to stack

提交回复
热议问题