Ad

FindNavController().navigate(direction) Does Not Work For View.OnLongClickListener, But It Works For View.OnClickListener

Android Developer Canary 3.4, kotlin.

Found that View.OnLongClickListener gives a type mismatch. Isn't View.OnLongClickListener not taken into consideration for the new Navigation Graph in Android ?

private fun createOnClickListener(stationId: String): View.OnClickListener
{
    return View.OnClickListener {
        val direction = StationListFragmentDirections.ActionStationListFragmentToStationDetailFragment(stationId)
        it.findNavController().navigate(direction)
    }
}

private fun createOnLongClickListener(stationId: String, kindId: String): View.OnLongClickListener
{
    return View.OnLongClickListener {
        val direction = StationListFragmentDirections.ActionStationListFragmentToUpdatePriceFragment(stationId,kindId)
        it.findNavController().navigate(direction)   // <--- Gives error here
    }
}

The above two functions should behave the same, but the lower (createOnLongClickListener) gives a 'Type mismatch' error for the 'direction'.

Isn't support for navigation added for View.OnLongClickListener ?

Ad

Answer

View.OnLongClickListener need return type as Boolean

Example:

val longClick = View.OnLongClickListener {

        [email protected] true
    }
Ad
source: stackoverflow.com
Ad