我有两个片段CommentsFragmentRegistrationFragmentCommentsFragment上,我保存了3 String数据类型。

这是我的评论片段

SingleTon singleTon;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_comment, container, false);
    ButterKnife.bind(this, view);
    Bundle args = getArguments();
    singleTon  = SingleTon.getInstance();
    singleTon.setContext(getContext());
    user = singleTon.getUser();
    id = args.getInt("id");
    slug = args.getString("slug");
    return view;
}


在这里,我将三个String数据保存在CommentsFragment中。

    @Override
    public void onSaveClicked(User user) {
        singleTon.setUser(user);
    }


这是我的SingleTon文件。

public class SingleTon {

private SharedPreferences preferences;

private static SingleTon singleTon;

public static SingleTon getInstance() {

    if (singleTon == null) {
        singleTon = new SingleTon();
    }

    return singleTon;

}

public void setContext(Context context) {
    preferences = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
}

public void setUser(User user) {
    setMail(user.getMail());
    setName(user.getName());
    setWeb(user.getWebSite());
}

public User getUser() {
    User user = new User();
    user.setWebSite(getWeb());
    user.setName(getName());
    user.setMail(getMail());
    return user;
}

private void setMail(String mail) {
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("mail", mail);
    editor.apply();
}

private void setName(String name) {
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("name", name);
    editor.apply();

}

private void setWeb(String web) {
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("web", web);
    editor.apply();
}

private String getName() {
    return preferences.getString("name", null);
}

private String getWeb() {
    return preferences.getString("web", null);

}

private String getMail() {
    return preferences.getString("mail",null);
}


}

我想在我的RegistrationFragment中代表我所有的三个String数据,这就是我实现它的方式。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_registration, container, false);
    singleTon = SingleTon.getInstance();
    singleTon.setContext(getContext());
    user = singleTon.getUser();
    setEditText();

    return view;
}

private void setEditText() {
    if (user.getWebSite() != null) {
        edit_web.setText(user.getWebSite());
    }
    if (user.getName() != null) {
        edit_name.setText(user.getName());
    }
    if (user.getMail() != null) {
        edit_mail.setText(user.getMail());
    }
}


我的问题是,当我在CommentsFragment中保存三个日期并在editTexts中转到RegistrationFragment时,它不会显示我的数据,但是如果我关闭应用程序并运行它,则在我的RegistrationFragment中会显示所有三个数据值。请问如何解决?

最佳答案

尝试使用editor.commit();而不是editor.apply();

也有条件检查用户是否不为空。在下面的行之前。

if (user.getWebSite() != null) {

09-30 17:46