如何在片段之间传递字符串? [英] How can I pass a String between fragments?

查看:71
本文介绍了如何在片段之间传递字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有一些问题,但是没有一个答案对我有用.

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

我想将String inputEmail从StartFragment传递到SignInFragment.

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;

我已经发现问题是找不到密钥,这就是应用程序始终崩溃的原因.所以我放了一个if子句来修复它,但是我仍然没有得到String inputEmail.

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.

所以我该如何从StartFragment SignInFragment传递字符串inputMail

预先感谢您

推荐答案

当您传递参数时,其类型也必须为String.而是,您传递一个Serializable类型,然后尝试检索一个String类型.请按如下所示更改您的代码:

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

这篇关于如何在片段之间传递字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆