How to reference the current or main activity from another class

后端 未结 13 802
难免孤独
难免孤独 2021-01-30 12:39

I often find myself needing to access methods that require referencing some activity. For example, to use getWindowManager, I need to access some Activity. But ofte

相关标签:
13条回答
  • 2021-01-30 13:21

    You can make you application instance a singleton, and use it when you need a Context

    An example is in this question:
    Android Application as Singleton

    This way, when you need a Context, you can get it with
    Context context = MyApplication.getInstance()

    This might not be the cleanest solution, but it has worked well for me so far

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