简体   繁体   中英

SharedPreferences. Android, Java

I'm sorry, I have a really newby question, but I think it's ok since I'm new in Android developing (in developing at all). I'm learning how to save my Preferences now. I have a simple application:

package com.foxysoft.prefssimple;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    final static String LOG_TAG = "myLogs";

    TextView tvInfo;
    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvInfo = (TextView) findViewById(R.id.tvInfo);

        sp = PreferenceManager.getDefaultSharedPreferences(this);
    }

    @Override
    protected void onResume() {
        Log.d(LOG_TAG, "onResume");
        Boolean notif = sp.getBoolean("notif", false);
        String address = sp.getString("address", "");
        String text = "Notifications are " + ((notif) ? "enabled, address = " + address : "disabled");
        tvInfo.setText(text);
        super.onResume();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem mi = menu.add(0, 1, 0, "Prefs");
        mi.setIntent(new Intent(this, PrefActivity.class));
        return super.onCreateOptionsMenu(menu);
    }
}

As you can see, I have a small menu and Activity for my Preferences (PrefActivity.class) and prefs.xml for my Preferences activity:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:key="notif"
        android:summary="Enable notifications"
        android:title="Notifications">
    </CheckBoxPreference>
    <EditTextPreference
        android:key="address"
        android:summary="Address for notifications"
        android:title="Address">
    </EditTextPreference>
</PreferenceScreen>

So, my question is. I have two lines in my onResume:

Boolean notif = sp.getBoolean("notif", false);
String address = sp.getString("address", "");

If I understand it right, it should do my "notif" checkbox false and "address" string "" (empty) everytime onResume called.. Or?.. If no - why? These two line are definitely saying "do notif false and address empty", am I right?

No, you are not right - as you are reading your stored preferences.

When there is no preference named "notif"; then the default that you provide to that method call is used; and false will be returned. But if there is a saved preference "notif", then the value saved for that will be returned.

Just read the corresponding javadoc for that method; it says:

defValue boolean: Value to return if this preference does not exist.

Returns the preference value if it exists, or defValue.

In other words: if you stored true before, then that value will be returned.

Besides, the real answer here is: there is no need on your side to assume how Android API calls work - all of that is nicely documented. So when in doubt, turn to the Javadoc. (and of course - although we try to be really accurate when answering questions - you still could receive a wrong answer here. the only thing that really matters is what the API docs say!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM