Search code examples
androidviewkotlinandroid-navigationonlongclicklistener

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 ?


Solution

  • View.OnLongClickListener need return type as Boolean

    Example:

    val longClick = View.OnLongClickListener {
    
            return@OnLongClickListener true
        }