0

I am working on an android app and I ask the user a question at first. Lets say male or female.. My app has two different parts, one is for men one is for women. If the user chooses "male", I open the activity for men. I put a checkbox to remember this choice for next time. And when the user opens the app next time I dont want to ask again, just open the activity for men.

I hope it is clear. Here is code piece of related part:

View checkBoxView = View.inflate(this, R.layout.activity_menu, null);
        CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox);
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                // Shared preferences.
            }
        });
        checkBox.setText("Bu kararımı sonraki girişlerde hatırla.");

Can you give me some inspiration? I checked ome questions and articles but I couldnt find an example such as mine.

Thanks in advance.

4

1 回答 1

2

你为什么不使用SharedPreference它会存储价值,你可以稍后使用它,即使应用程序崩溃也会保留。

例子 :

用于存储数据

SharedPreferences.Editor prefEditor = getPreferences(MODE_PRIVATE).edit();
prefEditor.putString("MyStore", <Value>);
prefEditor.commit();
prefEditor.apply();

从 sharedpreference 中读取

SharedPreferences sp = getPreferences(MODE_PRIVATE);
String str = sp.getString("MyStore", "TheDefaultValueIfNoValueFoundOfThisKey");
于 2013-09-21T15:27:41.870 回答