Baptiste Arnaud
Baptiste Arnaud

Reputation: 2750

passing EditText values from Fragment to Activity

I have a sign up form in a fragment contained in an Activity. I need to implement a function in the main Activity when the submit button is clicked in the fragment. by implementing this line :

android:onClick="signUp"

It goes directly into the Activity. But how can I get the Fragment values ? It's complicated because my Fragment doesn't have an idea because I switch different Fragments in the same spot.

What I tried :

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_sign_up, container, false);
    email =(EditText) v.findViewById(R.id.emailEt);
    mListener.setEmail(email.getText().toString());
    pass1 = (EditText) v.findViewById(R.id.passEt1);
    mListener.setPass1(pass1.getText().toString());
    pass2 = (EditText) v.findViewById(R.id.passEt2);
    mListener.setPass2(pass2.getText().toString());
    pseudo = (EditText) v.findViewById(R.id.pseudoEt);
    mListener.setPseudo(pseudo.getText().toString());
    signup =(Button) v.findViewById(R.id.signUpB);

    return v;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onSignUpFragmentInteraction(Uri uri);
    public void setEmail(String email);
    public void setPass1(String pass1);
    public void setPass2(String pass2);
    public void setPseudo(String pseudo);
}

Then in my Activity :

@Override
public void setEmail(String email) {

}

@Override
public void setPass1(String pass1) {

}

@Override
public void setPass2(String pass2) {

}

@Override
public void setPseudo(String pseudo) {

}

@Override
public void onLoginFragmentInteraction(Uri uri) {

}

But I don't know what to do now...

Upvotes: 1

Views: 1212

Answers (1)

Basti Destruction
Basti Destruction

Reputation: 397

A Fragment should always be reuseable and independend from its parent Activity. Your sign up form is in the Fragment, so you can read all values in your Fragment instance. Define the OnClickListener in the Fragment, instead of in the Activity. You can do validation and data processing in the Fragment and then pass all values to the parent Activity of the Fragment. A nice way to do this is to define a Interface in the Fragment and implement it in the Activity. See here.

The Interface would have a method like bool signUp(String username, String email, String password). To call this method from your Fragment you have to keep a reference to your parent activity cast to the Interface:

private OnFragmentInteractionListener mCallback;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

With that reference you can call the method of the Interface:

public void signUp(View v) {
    // get your values here
    mCallback.signUp(...);
}

If you inflate the layout with the onClick definition in your Fragment, make sure to implement the method in your Fragment.

Upvotes: 2

Related Questions