Search code examples
androidgoogle-mapscode-cleanupimplements

What to do with required, but empty, implementations?


I have a google map class

class MapActivity : FragmentActivity(), OnMapReadyCallback, LocationListener, GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

However i only use a few of the required functions provided by the implementations. All the unused methods i just place at the bottom of the class, like this:

override fun onConnected(p0: Bundle?) {
    checkLocationPermission()
    val mLocationProvider = FusedLocationProviderClient(this)

    mLocationProvider.lastLocation.addOnCompleteListener { task ->
        goToLocation(task.result.latitude, task.result.longitude)
    }

}

override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
    //To change body of created functions use File | Settings | File Templates.
}

override fun onProviderEnabled(provider: String?) {
    //To change body of created functions use File | Settings | File Templates.
}

override fun onProviderDisabled(provider: String?) {
    //To change body of created functions use File | Settings | File Templates.
}

override fun onConnectionSuspended(p0: Int) {
    //To change body of created functions use File | Settings | File Templates.
}

override fun onConnectionFailed(p0: ConnectionResult) {
    //To change body of created functions use File | Settings | File Templates.
}

Is there a better way to dismiss of these unwanted required functions?


Solution

  • No you can't do anything (except create another class as suggested...), this the typcal tradeoff of the Interface Segregation Principle.

    Anyway if you need to listen for location change update use

    import com.google.android.gms.location.LocationListener;
    

    that tell you to implement only

    void onLocationChanged(Location var1);
    

    I also suggest you to use #region and collapse code on your editor to clean visually your class.

    Hope it helps.