У меня есть пара флажков, которые мне нужно сохранить, чтобы при повторном открытии приложения они могли видеть состояние, в котором они оставили приложение. Я попытался использовать предпочтения, но я не могу получить результат правильно.Сохранить состояние CheckBox в SharedPreferences на Android
MainActivity.java
package com.example.android.documentchecklist;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
public class MainActivity extends AppCompatActivity {
CheckBox allotment, sscMarkList, hscMarkList, leaving, profomaCap, jeeScoreCard, gapCer, casteCer, casteVal, nonCreamyL, domicileCertificate, photograph, migration, adhaarCard, nationalityCertificate;
boolean hasAllotment, hasSscMarkList, hasHscMarkList, hasLeaving, hasProfoma, hasJeeScore, hasGapCertificate, hasCasteCertificate, hasCasteValidity, hasNonCreamyLayer, hasDomicileCertificate, hasPhoto, hasMigCertificate, hasadhaarCard, hasNationalityCertificate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
allotment = (CheckBox) findViewById(R.id.allot);
sscMarkList = (CheckBox) findViewById(R.id.ssc);
hscMarkList = (CheckBox) findViewById(R.id.hsc);
leaving = (CheckBox) findViewById(R.id.leaving);
profomaCap = (CheckBox) findViewById(R.id.profoma);
jeeScoreCard = (CheckBox) findViewById(R.id.jeeScore);
gapCer = (CheckBox) findViewById(R.id.gapCertificate);
casteCer = (CheckBox) findViewById(R.id.casteCertificate);
casteVal = (CheckBox) findViewById(R.id.casteValidity);
nonCreamyL = (CheckBox) findViewById(R.id.nonCreamyLayer);
adhaarCard = (CheckBox) findViewById(R.id.adhaarCard);
nationalityCertificate = (CheckBox) findViewById(R.id.nationalityCer);
domicileCertificate = (CheckBox) findViewById(R.id.domicile);
photograph = (CheckBox) findViewById(R.id.photo);
migration = (CheckBox) findViewById(R.id.migration);
}
public void checkBoxClicked(View view) {
int id = view.getId();
if (id == R.id.mahaState) {
migration.setVisibility(View.GONE);
allotment.setText(getString(R.string.allot));
allotment.setVisibility(View.VISIBLE);
hasAllotment = allotment.isChecked();
sscMarkList.setText(getString(R.string.ssc));
sscMarkList.setVisibility(View.VISIBLE);
hasSscMarkList = sscMarkList.isChecked();
hscMarkList.setText(getString(R.string.hsc));
hscMarkList.setVisibility(View.VISIBLE);
hasHscMarkList = hscMarkList.isChecked();
leaving.setText(getString(R.string.leaving));
leaving.setVisibility(View.VISIBLE);
hasLeaving = leaving.isChecked();
profomaCap.setText(getString(R.string.proforma));
profomaCap.setVisibility(View.VISIBLE);
hasProfoma = profomaCap.isChecked();
jeeScoreCard.setText(getString(R.string.jee));
jeeScoreCard.setVisibility(View.VISIBLE);
hasJeeScore = jeeScoreCard.isChecked();
gapCer.setText(getString(R.string.gap_cert));
gapCer.setVisibility(View.VISIBLE);
hasGapCertificate = gapCer.isChecked();
casteCer.setText(getString(R.string.caste_cert));
casteCer.setVisibility(View.VISIBLE);
hasCasteCertificate = casteCer.isChecked();
casteVal.setText(getString(R.string.caste_validity));
casteVal.setVisibility(View.VISIBLE);
hasCasteValidity = casteVal.isChecked();
nonCreamyL.setText(getString(R.string.non_creamy));
nonCreamyL.setVisibility(View.VISIBLE);
hasNonCreamyLayer = nonCreamyL.isChecked();
adhaarCard.setText(getString(R.string.aadhar));
adhaarCard.setVisibility(View.VISIBLE);
hasadhaarCard = adhaarCard.isChecked();
nationalityCertificate.setText(getString(R.string.nationality_cert));
nationalityCertificate.setVisibility(View.VISIBLE);
hasNationalityCertificate = nationalityCertificate.isChecked();
domicileCertificate.setText(getString(R.string.domicile));
domicileCertificate.setVisibility(View.VISIBLE);
hasDomicileCertificate = domicileCertificate.isChecked();
photograph.setText(getString(R.string.photos));
photograph.setVisibility(View.VISIBLE);
hasPhoto = photograph.isChecked();
}
}
@Override
public void onPause() {
super.onPause();
save(allotment.isChecked());
save(sscMarkList.isChecked());
save(hscMarkList.isChecked());
save(leaving.isChecked());
save(profomaCap.isChecked());
save(jeeScoreCard.isChecked());
save(gapCer.isChecked());
save(casteCer.isChecked());
save(casteVal.isChecked());
save(nonCreamyL.isChecked());
save(domicileCertificate.isChecked());
save(photograph.isChecked());
save(migration.isChecked());
save(adhaarCard.isChecked());
save(nationalityCertificate.isChecked());
}
@Override
public void onResume() {
super.onResume();
allotment.setChecked(load());
sscMarkList.setChecked(load());
sscMarkList.setChecked(load());
hscMarkList.setChecked(load());
leaving.setChecked(load());
profomaCap.setChecked(load());
jeeScoreCard.setChecked(load());
gapCer.setChecked(load());
casteCer.setChecked(load());
casteVal.setChecked(load());
nonCreamyL.setChecked(load());
domicileCertificate.setChecked(load());
photograph.setChecked(load());
migration.setChecked(load());
adhaarCard.setChecked(load());
nationalityCertificate.setChecked(load());
}
private void save(final boolean isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check", isChecked);
editor.apply();
}
private boolean load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
return sharedPreferences.getBoolean("check", false);
}
}
Спасибо за вашу помощь.
Это сработало! Я никогда раньше не использовал SharedPreferences. Спасибо за быстрый ответ. Я просто изменил определение своего метода и использовал разные клавиши для сохранения состояния флажков. –
@VarunJoshi Я рад, что могу помочь, счастливое кодирование :) –