Answer the question
In order to leave comments, you need to log in
SharePreferences not saved?
Hi all!
I'm confused, I'm sitting and my head hurts already :)
I'm studying such a phenomenon as SharePreferences, it is necessary to save three values on pressing a button.
Delivered on a default one of values. When I run the application, the value is not passed.
I press the button - the value is transferred. But I restart and it's gone.
In general, it is not possible to fix this. I just started to learn these saves, so don't swear too much that I can't understand such a thing :)
here is the code:
SharedPrefDialog.java
import android.content.Context;
import android.content.SharedPreferences;
public class SharedPrefDialog {
SharedPreferences mySharedPrefDialog;
public SharedPrefDialog(Context context) {
mySharedPrefDialog = context.getSharedPreferences("filename", Context.MODE_PRIVATE);
}
public void setDialogState(String state) {
SharedPreferences.Editor editor = mySharedPrefDialog.edit();
editor.putString("Dialog", state);
editor.commit();
}
public String loadDialogState () {
String state = mySharedPrefDialog.getString("Dialog", "1");
return state;
}
}
public class A1_1_Dialog extends AppCompatActivity {
private ViewFlipper viewFlipper;
final String TAG = "States";
SharedPrefDialog sharedprefdialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
sharedprefdialog = new SharedPrefDialog(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.a1_1_dialog_screen); //назначаем layout (внешний вид)
viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
if(sharedprefdialog.loadDialogState() == "1") {
// Создаем View и добавляем их в уже готовый flipper
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int layouts[] = new int[]{ R.layout.a1_1_1_first_dialog_new, R.layout.a1_1_2_second_dialog, R.layout.a1_1_3_three_dialog };
for (int layout : layouts)
viewFlipper.addView(inflater.inflate(layout, null));
}else if(sharedprefdialog.loadDialogState() == "2"){
// Создаем View и добавляем их в уже готовый flipper
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int layouts[] = new int[]{ R.layout.a1_1_1_first_dialog_new_1, R.layout.a1_1_2_second_dialog, R.layout.a1_1_3_three_dialog };
for (int layout : layouts)
viewFlipper.addView(inflater.inflate(layout, null));
}else if(sharedprefdialog.loadDialogState() == "3"){
// Создаем View и добавляем их в уже готовый flipper
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int layouts[] = new int[]{ R.layout.a1_1_1_first_dialog, R.layout.a1_1_2_second_dialog, R.layout.a1_1_3_three_dialog };
for (int layout : layouts)
viewFlipper.addView(inflater.inflate(layout, null));
}
Button button_dialog_1 = (Button) findViewById(R.id.button_dialog_1);
Button button_dialog_2 = (Button) findViewById(R.id.button_dialog_2);
Button button_dialog_3 = (Button) findViewById(R.id.button_dialog_3);
button_dialog_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sharedprefdialog.setDialogState("1");
Toast.makeText(getBaseContext(), "1 схема", Toast.LENGTH_SHORT).show();
}
});
button_dialog_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sharedprefdialog.setDialogState("2");
Toast.makeText(getBaseContext(), "2 схема", Toast.LENGTH_SHORT).show();
}
});
button_dialog_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sharedprefdialog.setDialogState("3");
Toast.makeText(getBaseContext(), "3 схема", Toast.LENGTH_SHORT).show();
}
});
loadDialogStatedoes not pass value to
setDialogState. How to fix?
Answer the question
In order to leave comments, you need to log in
Changed String to Int and FIG knows, but somehow it worked. On the first attempt, something blabbed and wrote in the logs that there was some kind of problem with string and int. I rearranged the value in the application and it opened, it even started to save. I reinstalled the application, reset the cache and somehow the pancake began to function normally without errors.
Oh, this wonderful world of nature and bugs.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question