Save variable in SharedPreferences based on which

2019-09-07 01:37发布

Im trying to make an EULA for my app, but there is a different EULA for the different countries we work with.

my idea was that i save a String in SharedPreferences like ZA for South Africa and KE for Kenya. So if you click the South Africa button, it will save the string in SharedPreferences as ZA and the same for Kenya. Once the button has been clicked it the new activity will then load the appropriate EULA by pulling the ZA or KE string from the SharedPreferences. This is what i have at the moment:

Country_select.java

public class country_select extends Activity {


private static final String TAG = "Logs";
public static final String PREFS_NAME = "PrefsFile";

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

    final Button za_button = (Button) findViewById(R.id.btn_za);
    Button ke_button = (Button) findViewById(R.id.btn_ke);
    Log.i(TAG, "created buttons");

    za_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("Country_Selected", "ZA");
            editor.commit();
        }
    });

    Log.i(TAG, "set button settings for ZA");
    ke_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("Country_Selected", "KE");
            editor.commit();
        }
    });
    Log.i(TAG, "set button settings for KE");
}

I may have this totally incorrect but on the layout file there are 2 buttons, one for KE and one for ZA.

I would like it, when the new activity is loaded to read SharedPreferences whether it has ZA or KE? Is what i have done here correct?

Thank you

3条回答
\"骚年 ilove
2楼-- · 2019-09-07 02:03

I think you're better off with using IntentExtras, in your first activity upon clicking the country button store the value inside a variable and when you want to start the new activity pass the data as an intent extra:

Intent intent= new Intent(getActivity(), NewActivity.class);
intent.putExtra("country", countryCode);
startActivity(intent);

And then inside the new activity you can retrieve the value like this:

String countryCode = getIntent().getExtras().getString("country");
查看更多
做自己的国王
3楼-- · 2019-09-07 02:03

Yes, the storing part is correct. Now you will need to access the stored value in your new activity.

Example-

SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
String storedCountry = sharedpreferences.getString("Country_Selected"); // could be null value if there is no value stored with Country_Selected tag.
查看更多
爷、活的狠高调
4楼-- · 2019-09-07 02:19

In order to maintain Shared preference across the application i use it this way , take a look

I saved a AppPrefes class as a seprate class in the package

public class AppPrefes {

    private SharedPreferences appSharedPrefs;
    private Editor prefsEditor;

    public AppPrefes(Context context, String Preferncename) {
        this.appSharedPrefs = context.getSharedPreferences(Preferncename,
                Activity.MODE_PRIVATE);
        this.prefsEditor = appSharedPrefs.edit();
    }

    /****
     * 
     * getdata() get the value from the preference
     * 
     * */
    public String getData(String key) {
        return appSharedPrefs.getString(key, "");
    }

    public Integer getIntData(String key) {
        return appSharedPrefs.getInt(key, 0);
    }

    /****
     * 
     * SaveData() save the value to the preference
     * 
     * */
    public void SaveData(String Tag, String text) {
        prefsEditor.putString(Tag, text);
        prefsEditor.commit();
    }

    public void SaveIntData(String key, int value) {
        prefsEditor.putInt(key, value);
        prefsEditor.commit();
    }

    /**
     * delete all AppPreference
     */
    public void deleteAll() {
        this.prefsEditor = appSharedPrefs.edit();
        this.prefsEditor.clear();
        this.prefsEditor.commit();
    }

In your Activity or Fragment were you would like to get or save data just use it like this

Decalre an object for the AppPrefes class

AppPrefes appPref;

Initialize in onCreate

appPref = new AppPrefes(getActivity(), "type name of your preference");

To save data to the preference use

appPref.SaveData("tag_name", "value to be saved);

To get data from the preference

appPref.getData("tag_name");

You can also save Integer values and clear all preference values if necessary just call the apporopriate methods.

查看更多
登录 后发表回答