I'm trying to store multiple variables from various EditText and Textview Boxes in a shared Preference which I has a specific name (so no default shared Preferences possible). Saving from Activity C and later retrieving works fine. However, if I save them in the Fragment of Activity D and try to retrieve it later, I get a blank field. Even the default value isn't shown. It must have something to do with the fragment, but I couldn't find the right syntax to get it to work. This answer (Shared Preferences in Fragment among others) couldn't help me (since it is using default shared Preferences).
Here is part of the on create method of Activity C where the shared preferences are committed
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_produktspezifikation);
//region BUTTON DEKLARATION
Button neuesWST = (Button) findViewById(R.id.btn_Neu_WST);
neuesWST.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivity(new Intent(C_Produktspezifikation.this,WST_bearbeiten.class));
}
});
Button Weiter = (Button) findViewById(R.id.btn_Produktspezifikation_weiter);
Weiter.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//region DATEN SPEICHERN
//Variablen definieren, um die Daten aus den Feldern auszulesen
TextView WstName = (TextView) findViewById(R.id.tv_WST);
TextView Schnittweg = (TextView) findViewById(R.id.tv_Schnittweg_Eintrag);
EditText Stueckzahl = (EditText) findViewById(R.id.et_Stuekzahl_Eintrag);
EditText Losgroesse = (EditText) findViewById(R.id.et_Losgroesse_Eintrag);
//Shared Preferences definieren
SharedPreferences sharedpref = getSharedPreferences("Datenspeicher", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpref.edit();
//Daten in benannter SharedPreference speichern
editor.putString("wstname", WstName.getText().toString());
editor.putString("schnittweg", Schnittweg.getText().toString());
editor.putString("stueckzahl", Stueckzahl.getText().toString());
editor.putString("losgroesse", Losgroesse.getText().toString());
editor.commit();
//endregion
startActivity(new Intent(C_Produktspezifikation.this,D_Maschine.class));
}
});
Except of fragment of Activity D (The other is identical) where more preferences are safed:
public class Maschine_v1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_maschine_variante_1, container, false);
//Variablen definieren, um die Daten aus den Feldern auszulesen
TextView maschinenname_v1 = (TextView) rootView.findViewById(R.id.tv_m_v1_Maschine);
TextView anschaffungswert_v1 = (TextView) rootView.findViewById(R.id.tv_m_v1_Anschaffungswert_Eintrag);
TextView raumbedarf_v1 = (TextView) rootView.findViewById(R.id.tv_m_v1_Raumbedarf_Eintrag);
TextView el_Anschlusswert_v1 = (TextView) rootView.findViewById(R.id.tv_m_v1_el_Anschlusswert_Eintrag);
//Shared Preferences definieren
SharedPreferences sharedpref = this.getActivity().getSharedPreferences("Datenspeicher", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpref.edit();
//Daten in benannter SharedPreference speichern
editor.putString("maschinenname_v1", maschinenname_v1.getText().toString());
editor.putString("anschaffungswert_v1", anschaffungswert_v1.getText().toString());
editor.putString("raumbedarf_v1", raumbedarf_v1.getText().toString());
editor.putString("el_anschlusswert_v1", el_Anschlusswert_v1.getText().toString());
editor.commit();
And a Fragment of Activity E where the shared Preferences from Activity D are supposed to be retrieved and aren't shown: anschaffungswert_v1 comes from activity D (or rahter it's fragment) and doesn't work.
Schnittweg comes from activity C and works.
public class Werkzeug_v1 extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_werkzeug_variante_1, container, false);
TextView tv = (TextView) rootView.findViewById(R.id.tv_v1_Werkzeug_Abnutzung_Eintrag);
TextView tv2 = (TextView) rootView.findViewById(R.id.tv_v1_Werkzeug_Schnittweg_Eintrag);
SharedPreferences sharedPref = this.getActivity().getSharedPreferences("Datenspeicher", Context.MODE_PRIVATE);
String test = sharedPref.getString("anschaffungswert_v1","N/A");
String test2 = sharedPref.getString("schnittweg", "");
tv.setText(test);
tv2.setText(test2);
Here is the Datenspeicher.xml
(my shared Preferences file) after saving in Activity D:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="anschaffungswert_v1"></string>
<string name="maschinenname_v1"></string>
<string name="schnittweg">1200</string>
<string name="el_anschlusswert_v1"></string>
<string name="losgroesse"></string>
<string name="wstname">Werkstück 1</string>
<string name="stueckzahl">33</string>
<string name="raumbedarf_v1"></string>
</map>