How to access context in inactivity classes

I am writing my first Android application and have some problems with passing context objects. A lot of methods / constructors seem to require the current context (activity), which causes me some problems in my action listeners. Instead of all my listeners and handlers being anonymous classes in the activity class, I created classes for every event that I would like to handle. For example, the buttonRegister button will be initialized as:

buttonRegister.setOnClickListener(new RegisterButtonClickAction());

However, I do not have access to the current context object inside my listener. I could pass the current context as a constructor parameter, but I'm not sure if this is the best choice. What is the โ€œbest practiceโ€ for creating handlers / listeners in Android or for working with context objects in general?

Thanks
b3n

+3
source share
1 answer

Just pass the current context in the handler constructor.

. , - - , - , ( , , TextView, ). , , , , contructor.

, , , , .

+1

Source: https://habr.com/ru/post/1769684/


All Articles