简体   繁体   中英

shared preferences Storing data in Activity A and retrieve data from Activity B

I am trying to save data using Shared Preferences and retrieving that data in another activity using TextView. Issue here is i am able to store data successfully but not able to retrieve data from another activity. What else do i need to change in my code to retrieve data from another activity..

EditText ed1,ed2,ed3;
Button b1;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
public static final String Phone = "phoneKey";
public static final String Email = "emailKey";
SharedPreferences sharedpreferences;

Main Activity



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

        ed1=(EditText)findViewById(R.id.editText);
        ed2=(EditText)findViewById(R.id.editText2);
        ed3=(EditText)findViewById(R.id.editText3);

        b1=(Button)findViewById(R.id.button);
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n  = ed1.getText().toString();
                String ph  = ed2.getText().toString();
                String e  = ed3.getText().toString();

                SharedPreferences.Editor editor = sharedpreferences.edit();

                editor.putString(Name, n);
                editor.putString(Phone, ph);
                editor.putString(Email, e);
                editor.commit();
                Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public void Second_layout(View view)
    {
        Intent i = new Intent(MainActivity.this, retrive.class);
        startActivity(i);

    }

retrieve.java



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrive);
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        TextView tv =(TextView)findViewById(R.id.textView11);
        sharedpreferences  = getSharedPreferences("MyPREFERENCES",0);
        String userString = sharedpreferences.getString("Name","Nothing Found");
        tv.setText(userString);

        }

You are using wrong keys in getSharedPreferences() and getString() methods.Also, the mode must be same.

You're supposed to do this :

sharedpreferences  = getSharedPreferences(MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);
String userString = sharedpreferences.getString(MainActivity.Name,"Nothing Found");
tv.setText(userString);

Two Things make sure that the shared preferences name "My Preferences" is common in both ways and cross if you are storing and retrieved in same manner as followed:

Storing:

 public static void setUserInfo(Context context, Map<String,Object> userInfo){
        SharedPreferences.Editor editor = context.getSharedPreferences(USER_PREFERENCES, context.MODE_PRIVATE).edit();
        editor.putString(USER_INFO,"My Data");
        editor.apply();
    }

Fetching:

public static Map<String,Object> getUserInfo(Context context){
        SharedPreferences prefs = context.getSharedPreferences(USER_PREFERENCES, context.MODE_PRIVATE);
        String userInfo = prefs.getString(USER_INFO,"");
        return userInfo;
    }

Comment below if you have any doubt.

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