Pass values from a Fragment to an Activity

Asked

Viewed 142 times

0

I want to pass the value of an Edittext (which is in a Fragment) to an attribute of an Activity object, but I am not succeeding, Toast from Activity returns null. Follow the code of the Fragment:

public class PerguntaUmFragment extends SlideFragment {
    private PerguntaUmInterface perguntaUmInterface;
    private EditText editMensagem;


    public PerguntaUmFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_pergunta_um, container, false);

        editMensagem = view.findViewById(R.id.editMensagem);

        perguntaUmInterface.setMensagem(editMensagem.getText().toString());


        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            perguntaUmInterface = (PerguntaUmInterface) context;
        } catch (Exception e) {
            Log.e("ERROR", e.toString());
        }
    }

    public interface PerguntaUmInterface {
        void setMensagem(String mensagem);
    }

    public void setPerguntaUmInterface(PerguntaUmInterface perguntaUmInterface) {
        this.perguntaUmInterface = perguntaUmInterface;
    }
}

Activity:

public class OcorrenciaActivity extends IntroActivity implements PerguntaUmFragment.PerguntaUmInterface {
    private final Ocorrencia ocorrencia = new Ocorrencia();

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(OcorrenciaActivity.this,
                "Mensagem: "+ ocorrencia.getMensagem(),
                Toast.LENGTH_SHORT).show();
     }
    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        if (fragment instanceof PerguntaUmFragment) {
            PerguntaUmFragment perguntaUmFragment = (PerguntaUmFragment) fragment;
        }

    }
    @Override
    public void setMensagem(String mensagem) {
        ocorrencia.setMensagem(mensagem);

    }
  • Because you just don’t send a Oast straight from the Fragment?

  • No, Toast is just a way to check if the message arrived correctly, it just printed "null" on the screen.

1 answer

0

You can use the class SharePreference to share the information for that purpose or use two other methods described below:

Creating a Sharedpreferences

SharedPreferences pref = getApplicationContext().getSharedPreferences("DATA", MODE_PRIVATE); 
Editor editor = pref.edit();
editor.putString("EDITEXT1", "string value");  // Salvando dados do seu editext
editor.commit(); // confirmar e salvar seus dados para a SharedPreferences

Now you can recover your data as in the code below:

SharedPreferences pref = getApplicationContext().getSharedPreferences("DATA", MODE_PRIVATE); 
String email=pref.getString("EDITEXT1", "");       // Salvando dados de string do seu editext

By Instance

Or you can define an instance field in your Activity and access it by going up the reference getActivity() to the MainActivity in the fragment:

((MainActivity) getActivity()).filedName.setFragmentOneData(data);

By Arguments

Lastly the method using Bandle to pass information through the fragment:

Bundle bundle = new Bundle();
bundle.putString("someEditTextData",someEditTextData);
SomeFragment fragment = new SomeFragment ();
fragment.setArguments(bundle);

And to recover the data in the fragment (adapting to your reality)

Bundle args = getArguments();
    if (args != null) {
        showReceivedData.setText(args.getString("someEditTextData"));
    }
  • I don’t know if it would be possible, but could it be based and apply in my code?? I really don’t know how to put this logic in the program

Browser other questions tagged

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