Needing Context in non-Activity classes

前端 未结 7 1670
轻奢々
轻奢々 2020-12-14 06:10

I have some classes within my application that need to call Android functions that require the Context as a parameter. I don\'t have it as the class is not a subclass of the

相关标签:
7条回答
  • 2020-12-14 07:03

    The best way is to follow Bean approach:

    public class Example {
        protected Context getContext() {
            ...
        }
    
        ...
    }
    

    Then it depends on possibilities to access context. If class is fully independent then constructor parameter and private field seems best approach.

    But that bean property way shields you from further code changes.

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