Ciao a tutti,
sto cercando di salvare un dato nelle Shared Preference e rileggerlo al prossimo avvio dell'applicazione.
Purtroppo sembra che il dato non venga salvato, infatti verifico in debug ed ogni volta prende il valore di defaullt.
Questo è il codice:
In lettura
codice:private void mostraToastPrimoAvvio() { //Verifico se il comune è già stato salvato... SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String resultComune = prefs.getString(COMUNE_SALVATO, "nessuno"); if(resultComune != "nessuno") { //Redirect a nuova activity /*Intent openPage1 = new Intent(this, Act2.class); startActivity(openPage1); */ AutoCompleteTextView itemTxtView = (AutoCompleteTextView) findViewById(R.id.autocomplete); itemTxtView.setText(resultComune); } }
e questo in scrittura
Qualche consiglio?codice:public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); //Recupero il bottone di invio dati e lo gestisco Button btnHome=(Button) rootView.findViewById(R.id.button1); final TextView txtValue = (TextView) rootView.findViewById(R.id.textView1); btnHome.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //Salvo il comune inserito SharedPreferences prefs = getActivity().getPreferences(MODE_PRIVATE); Editor prefsEditor = prefs.edit(); prefsEditor.putString(COMUNE_SALVATO, (String) txtValue.getText()); prefsEditor.commit(); Intent openPage1 = new Intent(getActivity(), Act2.class); startActivity(openPage1); } }); } });

Rispondi quotando

