-1

I know there are already some questions about it, but none of the answers work out for me.

I want to pass the String inputEmail from StartFragment to SignInFragment.

I tried to do this with the bundle:

StartFragment

SignInFragment fragmentTwo = new SignInFragment();
                Bundle bundle = new Bundle();
                bundle.putString("key", input_mail);
                fragmentTwo.setArguments(bundle);

SignInFragment:

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


    Bundle bundle = getArguments();
    if (bundle!=null) {
        String mail = bundle.getString("key");
    }
    else {
        Toast.makeText(getActivity(), "key not found", Toast.LENGTH_SHORT).show();
    }


    return view;

I have already found out that the problem is that the key couldn´t be found and that is the reason why app crashes all the time. So i put an if clause in in order to fix it but I still dont get the String inputEmail.

So how can I pass the String inputMail from StartFragment SignInFragment

Thank u in advance

Marek
  • 43
  • 1
  • 6

2 Answers2

1

When you pass an argument its type has to be String as well. Instead, you pass a Serializable type and then try to retrieve a String one. Please change your code as follows:

SignInFragment fragmentTwo = new SignInFragment();
            Bundle bundle = new Bundle();
            bundle.putString("key", input_mail); // pass a String key, not a Serializable one
            fragmentTwo.setArguments(bundle);
Anatolii
  • 14,139
  • 4
  • 35
  • 65
0

you can pass your String using your activity:

create an interface in StartFragment and implement it on your activity.

StartFragment:

public class StartFragment extends Fragment {
     private OnSignInListener onSignInListener;


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

        if (onSignInListener != null)
            onSignInListener.onSignIn("email");
    }

    public void setOnSignInListener(OnSignInListener onSignInListener) {
        this.onSignInListener = onSignInListener;
    }

    public interface OnSignInListener{
        void onSignIn(String email);
    }
}

SignInFragment:

public class SignInFragment extends Fragment {

    private String email;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //...
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

your activity:

public class MainActivity extends AppCompatActivity implements StartFragment.OnSignInListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //...

        StartFragment startFragment = new StartFragment();
        startFragment.setOnSignInListener(this);

    }

    @Override
    public void onSignIn(String email) {
        SignInFragment signInFragment = new SignInFragment();
        signInFragment.setEmail(email);//set email

        //replace fragment
        //...
    }
}
Ehsan msz
  • 1,774
  • 2
  • 13
  • 26