How do you pass an activity value to a fragment?

Asked

Viewed 53 times

0

I’m trying to instantiate a method that returns a value of type int in a fragment, but when I show the value on the device screen Textview shows this: null.

Fragment code:

   public View onCreateView(/*@NonNull*/ LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, container, false);


        Valor valor = new Valor();

        // TXTVIDAS
        TextView vidas = view.findViewById(R.id.txtVidas);
        vidas.setText(String.valueOf("fgdfhgd" + valor.getVidas()));

        // Botão começar
        Button btncomecar = (Button) view.findViewById(R.id.btncomecar);
        btncomecar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MundosActivity.class);
                startActivity(intent);
            }
        });

        return view;

I tested and the method returns a value other than null:

D/Vidas encotradas: 4

I mean, there should be a four instead of "null".

1 answer

1

I will give you the method that is to pass as argument. There are other methods of communication through Listener or Viewmodel.

Finally, create a new fragment already with the constructor newInstance() ready. For that, go on:

File > New > Fragment > Fragment(Blank)

It is already a template that creates a fragment and a new layout, you can name it.

There is in it the builder called newInstance(String param1, String param2)

Change and leave as follows:

//nome do seu fragment
public static MeuFragmento newInstance(int param1) {
    MeuFragmento fragment = new MeuFragmento ();
    Bundle args = new Bundle();
    args.putInt(ARG_PARAM1, param1);
    fragment.setArguments(args);
    return fragment;
}

In the onCreate() of the Fragment let:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getInt(ARG_PARAM1);
    }
}

In the method onCreateView(), note that I pass the parameter value in textview:

   @Override
   public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.meu_fragmento, container, false);

        // TXTVIDAS
        TextView vidas = view.findViewById(R.id.txtVidas);

        vidas.setText(String.valueOf("fgdfhgd" + mParam1));

        // Botão começar
        Button btncomecar = (Button) view.findViewById(R.id.btncomecar);
        btncomecar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MundosActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }

Now in your Activity where initialize and call the fragment instead of using:

Fragment meuFragmento = new MeuFragmento();

use:

Fragment meuFragmento = MeuFragmento.newInstance(valor.getVidas());

I hope it helps you. Hug!

  • I can solve the problem otherwise, but thanks for the help!

Browser other questions tagged

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