-2

I want to send data from a fragment to an activity, but my current code does not work.

    TextView mTituloTv=view.findViewById(R.id.titulo_producto_regis);
    TextView mSubTituloTv=view.findViewById(R.id.subtitulo_producto_regis);
    TextView mPrecioTv=view.findViewById(R.id.precio_producto_regis);
    TextView mDescripcionTv=view.findViewById(R.id.descripcion_producto_regis);

    String mTitulo=mTituloTv.getText().toString();
    String mSubTitulo=mSubTituloTv.getText().toString();
    String mPrecio=mPrecioTv.getText().toString();
    String mDescripcion=mDescripcionTv.getText().toString();

    Intent intent=new Intent(getContext(),Producto_Detallado.class);

    intent.putExtra("mtitulo",mTitulo); 
    intent.putExtra("mdescripcion",mDescripcion); 
    intent.putExtra("msubtitulo",mSubTitulo); 
    intent.putExtra("mprecio",mPrecio);

    startActivity(intent);
Gift You
  • 21
  • 2
  • 6
    Possible duplicate of [How to send data from fragment to another activity?](https://stackoverflow.com/questions/46954409/how-to-send-data-from-fragment-to-another-activity) – majid ghafouri Oct 05 '19 at 05:27
  • Follow: [https://stackoverflow.com/questions/46954409/how-to-send-data-from-fragment-to-another-activity] – Avilash Oct 05 '19 at 05:52

1 Answers1

-1

You can use interface to send data from fragment to activity. You can create interface and a method in your fragment: Like this is your interface code-

interface YourInterface {
fun sendData(data:YourDataMODEL)
}

This is your method- Make a global object of your interface in fragment

private var listener : YourInterface? = null

fun setListener(yourInterface : YourInterface){
this.listener = yourInterface
}

Call your interface object where you want - listener.sendData(yourModel)

When you intialize your fragment in activity then call this method using fragment object-

val fragment = YourFragment()
fragment.setListener(this)

Now you can implements this interface method in your activity like this:

@override
fun sendData(data : YourDataMODEL){
// Do Something here
}

I think this is helpful to you

Jai kumar
  • 45
  • 7