Reputation: 199
I've read many threads on how to create and handle Button clicks using onClickListener. However, I couldn't find anything that solved this problem.
I have a class myActivity and I would like to create a second class myExtendedActivity which extends myActivity by adding some extra functionality. The class myActivity contains a lot of code which, for maintainability reasons, I don't want to duplicate. myExtendedActivity should behave exactly as myActivity with some extra function.
In particular in the onCreate method in myActivity I have the following code that add an onClickListener to my_button. The extended activity should just append a call to myExtraMethod.
myButton = (Button)findViewById(R.id.my_button);
myButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
[...] //a lot of code here
myExtraMethod();
}
});
Note that myExtraMethod simply broadcasts an Intent and it is not affected by any other part of the code in the Listener.
What I would like to do is to extend the OnClickListener in myExtendedActivity so that it first executes exactly the code written for myActivity and than myExtraMethod defined in myExtendedActivity.
I don't want to modify myActivity which should be completely unaware if myExtendedActivity is included in the project or not.
Thank you very much
Upvotes: 0
Views: 985
Reputation: 681
Completely without modifying myActivity is barely possible.
You could use something like this in MyActivity
(Some variant of the Template method pattern):
onCreate() {
myButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
myMethod();
}
});
}
protected void myMethod() {
[...] //a lot of code here
}
And then in MyExtendedActivty
:
@Override
protected void myMethod() {
super.myMethod();
myExtraMethod();
}
Upvotes: 2
Reputation: 4891
Without modifying myActivity
, you can't because you're instantiating an anonymous class, and the subclass has no way to "hook in" to that anonymous class.
If you can modify myActivity
, then you could make the anonymous class from myButton.setOnClickListener(...)
a static class, and in your subclass extend the listener class calling super.onClick(v)
, followed by the functionality you expect.
Then add a method to get the correct listener
myButton.setOnClickListener(createMyButtonsListener())
that you can then override in the subclass providing the extended version.
Alternatively, add a method that can be overriden in a subclass (i.e. myExtraMethod()
). that is called within your anonymous class.
Upvotes: 0
Reputation: 4427
If I understood your question right, the solution might be something like this:
In your layout for button you define the onClickListener from xml:
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="handleButtonClick"
/>
Then, in MyActivity you implement method:
public void handleClick(View view) {
}
And in MyExtendedActivity you override it:
public class MyExtendedActivity extends MyActivity {
@Override
public void handleClick(View view) {
super.handleClick(view);
//your code goes here
}
}
Upvotes: 0