简体   繁体   English

如何在一个片段中存储数据以在另一个片段中获取数据?

[英]How to store data in one fragment to get it in another fragment?

I am storing a string in my shared preference class and I want to use this String in another fragment. 我在共享首选项类中存储了一个字符串,并且我想在另一个片段中使用此字符串。

I think I am doing something wrong with the Context, Application Context. 我认为我在Context,Application Context中做错了。 I did not know about the context thing. 我不知道有关上下文的事情。 Whenever I open my Profile Fragment the app crashes. 每当我打开“个人资料片段”时,应用程序就会崩溃。

Shared Preference Class 共享首选项类别

public class SharedPrefs {
private static final String myPrefs = "myprefs";
private static final String LogedInKey = "Key";
private final Context context;
private final SharedPreferences sharedPreferences;
private final SharedPreferences.Editor editor;


public SharedPrefs(Context context) {
    this.context = context;
    sharedPreferences = context.getSharedPreferences(myPrefs, Context.MODE_PRIVATE);
    editor = sharedPreferences.edit();
}

public void savePref(String key) {
    editor.putString(LogedInKey, key);
    editor.commit();
}

public String getLogedInKey() {
    return sharedPreferences.getString(LogedInKey, null);
}

public void clearLogin() {
    editor.putString(LogedInKey, null);
    editor.commit();
}}

My Profile Fragment: 我的个人资料片段:

public class ProfileFragment extends Fragment {
private Context context;
private SharedPrefs sharedPrefs=new SharedPrefs(context);

public ProfileFragment(Context context) {
    this.context = context;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_profile, null);
    TextView userName=view.findViewById(R.id.userName);
    String a=sharedPrefs.getLogedInKey();
    userName.setText(a);

    return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


}}

I just want to set the string to the TextView from the shared reference. 我只想从共享引用将字符串设置为TextView。

1.Actually you don't need to pass context as fragment constructor cause that is not right and it will not work 1,实际上您不需要将上下文作为片段构造函数传递,因为这是不正确的,它将无法正常工作

    public class ProfileFragment extends Fragment {
    private SharedPrefs sharedPrefs;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    sharedPrefs=new SharedPrefs(getActivity());
    View view = inflater.inflate(R.layout.fragment_profile, null);
    TextView userName=view.findViewById(R.id.userName);
    String a=sharedPrefs.getLogedInKey();
    userName.setText(a);

      return view;
    }
    @Override

    public void onViewCreated(@NonNull View view, @Nullable Bundle                  
    savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);

   }}

Try using 尝试使用

In onCreateView() 在onCreateView()中

context = getActivity().getApplicationContext(); context = getActivity()。getApplicationContext();

define your sharedpreference in the constructor: 在构造函数中定义您的sharedpreference:

public ProfileFragment(Context context) {
this.context = context;
sharedPrefs=new SharedPrefs(context);
}

And be sure you save something with the key LogedInKey 并确保您使用LogedInKey键保存了一些内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM