Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

onAttachFragment esta deprecated

Olá pessoal.

Este metodo esta deprecated desde a API 28.

O que devo usar no lugar de:

 override fun onAttachFragment(fragment: Fragment?) {
        super.onAttachFragment(fragment)
        if(fragment is ListaProdutosFragment){

        }
    }
3 respostas

Fala Marco, de boa ?

Cara na documentação achei isso aqui:

The responsibility for listening for fragments being attached has been moved to FragmentManager. You can add a listener to getChildFragmentManager the child FragmentManager} by calling addFragmentOnAttachListener in onAttach to get callbacks when a child fragment is attached.

Eu tentei desta formar, mas não funcionou:

override fun onCreate(savedInstanceState: Bundle?){

       super.onCreate(savedInstanceState)
        setContentView(binding.root)
        val fm = supportFragmentManager
        val listener = FragmentOnAttachListener { _, fragment ->
            if (fragment is ListaProdutosFragment) {
                fragment.fabCadastraProduto = {
                    vaiParaFormularioProduto()
                }
                fragment.abreDetalhesProduto = {produto, recyclerView ->
                    abreDetalheProduto(produto,recyclerView)
                }
            }
        }

        supportFragmentManager.addFragmentOnAttachListener(listener)
    }
solução!

funcionou desta maneira:

 init {
        val fm = supportFragmentManager

        val listener = FragmentOnAttachListener { fragmentManager, fragment ->
            if (fragment is ListaProdutosFragment){
                fragment.abreDetalhesProduto = {produto, recyclerView ->
                    abreDetalheProduto(produto,recyclerView)
                }
                fragment.fabCadastraProduto = {
                    vaiParaFormularioProduto()
                }
            }
        }
        fm.addFragmentOnAttachListener(listener)

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

    }

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software