简体   繁体   中英

savedPreferences isn't being sent

This is my register code

    package com.synamegames.lolingaddress;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;

public class RegisterActivity extends Activity {
    TextView vname, vphone, vemail, vaddress, v_name,v_phone,v_email,v_address
    ,reg_name,reg_phone,reg_email,reg_address;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registermenu);
        vname=(TextView)findViewById(R.id.reg_name);
        vphone = (TextView) findViewById(R.id.reg_phone);
        vemail = (TextView) findViewById(R.id.reg_email);
        vaddress = (TextView) findViewById(R.id.reg_address);
        LoadPreferences();
    }

        private void LoadPreferences() {
            SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
            String strSavedMem1 = sharedPreferences.getString("MEM1", "");
            String strSavedMem2 = sharedPreferences.getString("MEM2", "");
            String strSavedMem3 = sharedPreferences.getString("MEM3", "");
            String strSavedMem4 = sharedPreferences.getString("MEM4", "");
            String deviceid = sharedPreferences.getString("Devid", "");
            vname.setText(strSavedMem1);
            vphone.setText(strSavedMem2);
            vemail.setText(strSavedMem3);
            vaddress.setText(strSavedMem4); // textfield

    ;
}

        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                Intent intent = new Intent(RegisterActivity.this, MainMenuActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);
                return super.onKeyDown(keyCode, event);
            }
            return super.onKeyDown(keyCode, event);
        }



    }

Mem 1 and the other strings were identified in my settings class and i have seen them load. Is sharedPreference's content shared in multiple classes?

Right now when i open this activity it shows me

Name: (Where preferences should be) Phone: (Where preferences should be) Email: (Where preferences should be) Address: (Where preferences should be)

(Where preferences should be) is blank and i want to have the text set to what i loaded.

Thanks for any help!

Code when saving the preferences

                        SavePreferences("MEM1", vname.getText().toString());
                    SavePreferences("MEM2", vphone.getText().toString());
                    SavePreferences("MEM3", vemail.getText().toString());
                    SavePreferences("MEM4", vaddress.getText().toString());

        private void SavePreferences(String key, String value) {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

getPreferences returns preferences private to this particular activity. If you set this preferences in another activity, say using PreferenceActivity , then they will not be available this way. You can use getDefaultSharedPreferences in this case.

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