How can I pass a String between fragments?

耗尽温柔 提交于 2019-12-23 06:21:01

问题


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


回答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);



回答2:


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
        //...
    }
}


来源:https://stackoverflow.com/questions/58680300/how-can-i-pass-a-string-between-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!