为什么sharedPreference不保存在这里?

时间:2018-06-30 08:06:03

标签: java android sharedpreferences

我试图在sharedPreferences中设置一些字符串,但是当我重新启动应用程序时,首选项为空,因此getString方法中返回“ null”。

apply plugin: 'com.android.application'
check.dependsOn 'assembleDebugAndroidTest'

android {
    compileSdkVersion 27

    defaultConfig {
        applicationId "com.google.firebase.quickstart.database"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
        }
    }
}

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:27.1.1'
}

dependencies {
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'

    implementation 'com.firebaseui:firebase-ui-database:4.0.0'

    implementation 'com.google.android.gms:play-services-auth:15.0.1'

    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-auth:16.0.2'
    implementation 'com.google.firebase:firebase-database:16.0.1'

    // Needed to fix a dependency conflict with FirebaseUI'
    implementation 'android.arch.core:runtime:1.1.1'

    // Needed to fix a dependency conflict with FirebaseUI'
    implementation 'android.arch.core:runtime:1.1.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
}

apply plugin: 'com.google.gms.google-services'

1 个答案:

答案 0 :(得分:1)

您拥有selectedCityselectedTheater这两个类别字段,它们从未更新,因此它们始终为null,但随后在onSaveInstanceState中保存了这些字段,因此当您进行活动时关闭,它将null写入SharedPreferences中的两个字段。当spnner更改时,您应该更新这些字段。

    citySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
        @Override
        public void onItemSelected(Spinner parent, View view, int position, long id) {
            selectedCity = citySpinner.getSelectedItem().toString(); // <--
            updateTheatersSpinner(selectedCity);
            mSettings.edit().putString("selectedCity", selectedCity).commit();
        }
    });

    theaterSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
        @Override
        public void onItemSelected(Spinner parent, View view, int position, long id) {
            selectedTheater = theaterSpinner.getSelectedItem().toString(); // <--
            mSettings.edit().putString("selectedTheater", selectedTheater).commit();
        }
    });

然后,在onSaveInstanceState

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    Log.d("testing" , "On save instancestate") ;
    super.onSaveInstanceState(outState);

    SharedPreferences preferences = this.getActivity().getPreferences(Context.MODE_PRIVATE);
    preferences.edit()
        .putString("selectedCity", selectedCity)
        .putString("selectedTheater", selectedTheater)
    .commit();
}