S
S
steve_fahrenheit2019-11-23 23:23:03
Java
steve_fahrenheit, 2019-11-23 23:23:03

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

dialog:
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));
        }

Buttons:
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();
            }
        });

In general, the feeling that in the SharedPrefDialog
loadDialogState
does not pass value to
setDialogState
. How to fix?
Thanks :)
ps and for some reason the word *public* lights up and writes: acces can be package private, as if it is not used in other classes, as I understand it? But it is used)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
steve_fahrenheit, 2019-11-24
@steve_fahrenheit

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 question

Ask a Question

731 491 924 answers to any question