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

本文是小编为大家收集整理的关于我如何在片段之间传递一个字符串?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我知道已经有一些问题,但是对我来说,没有任何答案.

我想将字符串输入邮件从startfragment传递到签名.

我试图用捆绑包:

startfragment

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

标志:

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子句来修复它,但我仍然没有获得字符串输入邮件.

因此,如何从开始fragpragment signfragment

传递字符串Inputmail

预先感谢您

推荐答案

通过参数时,其类型也必须为String.取而代之的是,您通过Serializable键入,然后尝试检索String一个.请按以下方式更改您的代码:

SignInFragment fragmentTwo = new SignInFragment();
            Bundle bundle = new Bundle();
            bundle.putString("key", input_mail); // pass a String key, not a Serializable one
            fragmentTwo.setArguments(bundle);

其他推荐答案

您可以使用活动传递字符串:

在StartFragment中创建一个接口并在您的活动上实现.

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

您的活动:

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://www.itbaoku.cn/post/2091057.html

问题描述

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

推荐答案

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

其他推荐答案

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