0

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?

Joel Broström
  • 3,530
  • 1
  • 34
  • 61
  • You could create a parent class to implement those interfaces, so your parent class will have those empty methods but the child class will be clean. – diegoveloper Nov 17 '17 at 15:56
  • @diegoveloper and then i just implement these classes instead? I suppose that's a solution :) Any one else? I was hoping for a easy quick fix to tell the class to just use the super implementation if not otherwise specified. – Joel Broström Nov 17 '17 at 16:01

2 Answers2

1

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.

appersiano
  • 2,670
  • 22
  • 42
0

You must implement all the methods of Interface.

But yes, in the case of code cleaning, you can have one extra class for implementing all methods of interface. And use that class with your original.

Click here for more info.

Dhruv Patel
  • 1,529
  • 1
  • 18
  • 27