0

I have the following two activity I want to send the values present in'name' and 'subdivision' present in the shared preference to the second activity. I am able to get the values using shared preference but I'm not able to send that value to the second activity. Below is my code...Can anyone help me resolve this ?

public class UidScreen extends Activity {
    EditText editrexts, edits, edites, editess;
    Button buttons;
    String userId;
    LayoutTwoActivity layoutTwoActivity;
    DatapushService dsp = null;
    CheckBox chkSignedIn;
    SharedPreferences settings;
    private static final String PREFRENCES_NAME = "myprefrences";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.uid_screen);
        editrexts = (EditText) findViewById(R.id.editText1);
        edits = (EditText) findViewById(R.id.editText2);
        edites = (EditText) findViewById(R.id.editText3);
        editess = (EditText) findViewById(R.id.editText4);
        chkSignedIn = (CheckBox) findViewById(R.id.chksingin);
        //userId = editrexts.getText().toString();
        settings = getSharedPreferences(PREFRENCES_NAME,
                Context.MODE_PRIVATE);
        String name = settings.getString("name", "");
        String division = settings.getString("Division", "");
        String Subdivision = settings.getString("Subdivison", "");
        String range = settings.getString("Range", "");


        editrexts.setText(name);
        edits.setText(division);
        edites.setText(Subdivision);
        editess.setText(range);


        buttons = (Button) findViewById(R.id.button1);

        buttons.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //LayoutTwoActivity ly=new LayoutTwoActivity();
                //dsp = new DatapushService();
                userId = editrexts.getText().toString();

                AssetDetails assetDetails = new AssetDetails();
                assetDetails.setUserID(userId);
                //ly.assetDetails =assetDetails;
                //dsp.assetDetails =assetDetails;
                Databaseuid ui = new Databaseuid(UidScreen.this);
                ui.open();
                String Division = ui.getDivision(userId);
                String Subdivision = ui.getSubdivision(userId);
                String Range = ui.getRange(userId);
                edits.setText(Division);
                edites.setText(Subdivision);
                editess.setText(Range);
                if (chkSignedIn.isChecked()) {

                    showToast("User Name and Password Saved!!!");

                    SharedPreferences settings = getSharedPreferences(
                            PREFRENCES_NAME, Context.MODE_PRIVATE);
                    settings.edit().putString("name", userId).putString("Division",Division).putString("Subdivison", Subdivision).putString("Range", Range).commit();

                    showToast("Restart App And see it works!!!");
                } else {
                    showToast("Tick keep me logged in!!!");

                }


                ui.close();
            }
        });


    }

    public void opennewactivity(View view) {
        // Do something in response to button !! Used to call MainActivity Class on click of the image !
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("str4", userId);


        startActivity(intent);
    }

    private void showToast(String msg) {
        Toast.makeText(UidScreen.this, msg, Toast.LENGTH_LONG).show();


    }
}

2nd Activity:

public class LayoutTwoActivity extends Activity {

String pref,pref2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.xml_second);
        //SharedPreference settings;
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        pref = settings.getString("name", "");
        pref2 = settings.getString("Subdivison",null);
        Toast.makeText(getApplicationContext(), pref,
                Toast.LENGTH_LONG).show();

}
        }
user3496326
  • 129
  • 1
  • 5
  • 13

4 Answers4

3

You're actually reading from two different preference files. Check this question.

these preferences

getSharedPreferences(PREFRENCES_NAME, Context.MODE_PRIVATE);

differ from these

PreferenceManager.getDefaultSharedPreferences(this);
Community
  • 1
  • 1
Enrichman
  • 11,157
  • 11
  • 67
  • 101
3

try this to set Shared Pref

String userId=yourstring,range=yourstring,subdivision=yourstring;
SharedPreferences settings = PreferenceManager
                    .getDefaultSharedPreferences(youractivity.this);
                Editor edit = settings.edit();
                edit.putString("name", userId);
                edit.putString("subdivision", subdivision);
                edit.putString("range", range);
                settings.commit;

to get shared pref

 SharedPreferences settins = PreferenceManager
            .getDefaultSharedPreferences(Youractivity.this);
 pref1=settings.getString("name", "anon");
 pref2=settings.getString("subdivision", "anon");
Clinton Dsouza
  • 330
  • 4
  • 20
0

Just did the following change and it worked, thanks Enrichman

SharedPreferences settings = getSharedPreferences("myprefrences", Context.MODE_PRIVATE);
        pref = settings.getString("name", "");
        pref2 = settings.getString("Subdivison",null);
user3496326
  • 129
  • 1
  • 5
  • 13
0
  1. In your first activity

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("NameOfShared", "Value");
    editor.commit();
    
  2. In your second activity

       final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String value=(mSharedPreference.getString("NameOfShared", "Default_Value"));
    
STF
  • 1,485
  • 3
  • 19
  • 36
Sunil
  • 3,785
  • 1
  • 32
  • 43