Is it possible to make a secondary class to hold the OnClick Listener? Meaning not being created in the Activity class?
I just find that putting OnClick listeners in the main activity class is just messy and I would rather have them in separate classes. Thanks
Instead of putting the
onCLicklistener
in a separate class, why dont you try to defineonClickListener
outsideonCreate()
??For e.g: like this
onCreate()
Outside onCreate()
Let me share how I code it using MVP. It's the best way to make a clean code. Remember each classes must have an interface to control it. I will show you with the simplest one.
Suppose you want to Toast a text onClick and control it from another class. Here's how it works. Creating interfaces are for nothing but to connect each other and you can review the code easily.
Create an interface for that MainActivity class.
Create another interface for Presenter class.
Remember interfaces are nothing but to override method for each classes.
Create Presenter class
I'll skip, activity_main.xml layout because there's just a button with id="@+id/buttonId." In MainActivityClass,
All I want to tell you is that. If you create objects in a class, it cannot make unit testing. That's why you're not seeing any new objects calling in android. So, you can use singleton pattern (Here is Lazy Type) in Presenter class. I'll remove its interface and Generic to see it clearly.
And so you can get its methods from MainActivity like this. Instead of creating objects like this...
You can get it like this...
More example for singleton pattern can be found here, https://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-examples
Finally, using static is not a very good choice because it uses memory space whether you use it or not. And so, you can create objects within Application Layer get it with a Typecasting. I'm sure you don't need to unit test that Application layer.
And you need to give a class name within Application in manifest.xml
And you can get it with a Typecast in MainActivity like this!
Yes you can. However, making the listener an inner class has one advantage - it can access the fields and variables of your activity class directly. If you make it a separate class, and your listener actually need to access 5 views, your listener constructor might look like this:
MyListener listener = new MyListener(context, button, textView1, textView2, ratingBar, imageView);
Which is kinda bulky too. If your listener is simple, go ahead and make it a separate class. Otherwise, its up to you for readability.
Sure, that's possible. Just create a class that implements
View.OnClickListener
and set that as listener to theView
. For example:And then set an instance of above class as listener:
The parameterized constructor is optional, but it's very likely you'll need to pass something through to actually make your
onClick(...)
logic work on.Implementing a class anonymously is generally easier to work with though. Just a thought.
You can do it. But just think that you will not have a reference to the activity, neither to it's attributes, including all the views. (unless you make them public or accessible with getters methods).
Also, be extra carefull with storing references to the activity or any members on the listener, since they might avoid the garbage collector from getting the listener memory back.