How do I pass Activity data to your Fragment?

Asked

Viewed 120 times

1

I learned how to send data from a Fragment to your Activity using interface. Only now I’m trying to do the opposite. Because I sent data to an Activity and I want that data to be sent back to your files.

...

Thank you guys, I’m sorry it took me so long to answer and not explain, I’m new here and this is my first question. I got it, I was using the Smarttablayout library to insert tabs into the Activity. Instead of adding a text and the Fragment class in Fragmentpageritemadapter, I just added a Fragmentpageitem already with the values I wanted to pass to Fragment. in this way:


FragmentPagerItem itemBranco = FragmentPagerItem.of("", BrancoFragment.class,
        new Bundler().putSerializable("passarTipo", tipoProduto).get());

//Configurar abas
FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
        getSupportFragmentManager(),
        FragmentPagerItems.with(this)
                .add(itemBranco)
                .create()
);

And in Fragment I picked up the values using onActivityCreated:


@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    //Pegar o tipoProduto do EstoqueActivity
    Bundle bundle = getArguments();

    tipoProduto = (TipoProduto) bundle.getSerializable("passarTipo");

    if (tipoProduto == null){
        Toast.makeText(getActivity(), "O tipo produto é nulo", Toast.LENGTH_LONG).show();
    }else {
        Toast.makeText(getActivity(), "Valor tipo produto passado com sucesso", Toast.LENGTH_LONG).show();
    }

}

I hope I can help anyone with the same problem by using this library, Hug.

  • welcome Felipe. please insert excerpt from your code so we can better understand the way you are trying to do.

  • Good morning Felipe, Go to this link https://answall.com/questions/137905/comond-dataos-de-uma-activity-para-um-fragment/137911#137911 and see if it suits you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.