0

I've created a shared preference screen on android to receive info from the shared preference screen to the main screen, every thing is working fine, but the the values in the preference screen aren't saved , and every time I must open the shared preference screen and type the info manualy ... any suggestions to auto save the preferences?

this is the shared perefrence code:

Button b1 = (Button)findViewById(R.id.SavePreff);
        b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String ID_NO = ID_NUMBER.getText().toString();
            SharedPreferences prefsExample = getSharedPreferences(PREFS, 0);
            SharedPreferences.Editor editor = prefsExample.edit();
            editor.putString("idNumber", ID_NO);

            if (rd1.isChecked())

                editor.putString("Network", "STC");

            else if (rd2.isChecked())
                editor.putString("Network", "Mobily");

            else 
                editor.putString("Network", "Zain");

            editor.commit();

            Intent i = new Intent(SharedPref.this , MainActivity.class);
            startActivity(i);
        }
    });

and this is the main activity screen that I'm using the info from it :

        b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            SharedPreferences example = getSharedPreferences(PREFS, 0);
            String ID_NUM = example.getString("idNumber", "123432");
            String NETWORK = example.getString("Network", "STC");
            String called_num = "";

            if (NETWORK.equals("Mobily"))
                called_num = "*1400*" + CODE_CHARGE.getText().toString() + "*"+ ID_NUM+ Uri.encode("#");
            else if (NETWORK.equals("STC"))
                called_num = "*155*" + CODE_CHARGE.getText().toString() + "*"+ ID_NUM+ Uri.encode("#");
            else if (NETWORK.equals("Zain"))
                called_num = "*141*" + ID_NUM + "*"
                        + CODE_CHARGE.getText().toString()+ Uri.encode("#");

            Intent i = new Intent("android.intent.action.CALL");
            Uri data = Uri.parse("tel:" + called_num);
            i.setData(data);
            startActivity(i);
        }
    });
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

Shared Preferences are like these two

//example1 for Shared Prefs that lasts forever
example1= PreferenceManager.getDefaultSharedPreferences(this);   
//example for Shared Prefs that lasts only just once each time program is running
example2= getApplicationContext().getSharedPreferences("PREFS", Activity.MODE_PRIVATE);

In your code, use

 example1= PreferenceManager.getDefaultSharedPreferences(this); 

Hope this works for you..

Lal
  • 14,726
  • 4
  • 45
  • 70
  • i have no getDefaultSharedPreferences(); in the options and I get error when writing it , and its not allow to use context – AbdullahADhaim Jul 07 '14 at 16:10
  • You please define the Shared preferences after `setContentView` in your `onCreate()`..So please move the code to `onCreate()` – Lal Jul 07 '14 at 16:12
  • SharedPreferences prefsExample = getDefaultSharedPreferences(this); is not working – AbdullahADhaim Jul 07 '14 at 16:18
  • The method getDefaultSharedPreferences(SharedPref) is undefined for the type SharedPref – AbdullahADhaim Jul 07 '14 at 16:33
  • it's too long , but could you give me a link for a solution or any project ?? – AbdullahADhaim Jul 07 '14 at 17:24
  • See this [link](http://stackoverflow.com/questions/18204708/preferencemanager-getdefaultsharedpreferences-vs-getpreferences) and this [link](http://stackoverflow.com/questions/5946135/difference-between-getdefaultsharedpreferences-and-getsharedpreferences) too.. – Lal Jul 07 '14 at 17:26
  • Or else check these [links](https://www.google.co.in/search?q=PreferenceManager.getDefaultSharedPreferences(this)&oq=PreferenceManager.getDefaultSharedPreferences(this)&aqs=chrome..69i57.1646j0j7&sourceid=chrome&es_sm=122&ie=UTF-8) – Lal Jul 07 '14 at 17:27