AFAIK You don’t need the anonymous inner class, you can just assign the lamba to a variable of the type AudioManager.OnAudioFocusChangeListener instead of the lambda’s type. That way it only gets wrapped once.

The problem is that the lamba is the same ‘shape’ as the listener, but it’s not the same type. That’s why the compiler cleverly wraps it when you assign it. It sees the shape is the same, but the type isn’t so it creates a wrapper to ensure the correct type. If you make your var of the correct type for the listener, it wraps it at that point.

The lambda is an anonymous inner class, but one that implements one of the FunctionN interfaces. When you use it as a listener, it has to be wrapped as a SomethingListener, which causes the new instance. When the FunctionN and SomethingListener interfaces have the same ‘shape’ (that is, the same number of variables and the same variable and return types) the compiler does this for you automatically. It’s a bit like autoboxing in this way.

The solution is to assign the lambda to an instance variable of the type SomethingListener instead the lambda’s type, that way it gets wrapped at the point where you create it.

It’s still a var though. So it makes sense two instances have to be made, because the compiler can’t be sure that the instance will be the same at both points (it could probably be made smart enough to figure it out, but it’s not surprising that it hasn’t). If that were a val, it’s possible things would be different.