I need a getOnClickListener() for Views in Android. This way I can assign a temporary OnClickListener to my Views.
I want to use it like this:
private View.OnClickListener oldListener;
public void assignTempListener(View view) {
oldListener = view.getOnClickListener(); // doesn't exist
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// some code
v.setOnClickListener(oldListener);
}
});
}
The problem is that this function doen’t exist. I also can’t inherit from View to create this method, because all kind of Views can be passed to assignTempListener. Is there another way to use this?
Edit: made a small mistake in my code.
You can do this with…REFLECTION. *drum roll*
This route is fraught with peril.
I don’t recommend it, as the internal structure of the class can change at any time, but here’s how you can do it currently up to Android 4.2.2 if it’s truly unavoidable: