I'm beginner in Android development and I have an issue with save changement with some texts display in some TextViews. But one is more difficult for me. For explain simple, I have 2 activities, Activity1 is the main, Activity2 is the user informations. In Activity1 (Main) the user clic the button ("login") then he go on Activity2 here user can enter his personal informations and he click on a button ("save data") then I add his datas on a database (SQLite), then when the user click on another button ("go") he return on Activity1 I use an intent in order to show his nickname on a TextView in the Activity1 with a "Welcome" + nickname. But when the user quit the app, there's no save, the changement hasn't save with the nickname, but the nickname are always in my database.
I will show u some code: Activity2:
public class ProfilActivity extends Activity {
EditText editNickname; // We just work on the nickname so I just keep it
MyDatabase myDb;
Button bAddData, go;
public SharedPreferences prefs;
private String nicktobesaved;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_profil);
myDb = new MyDatabase(this);
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
editNickname = (EditText) findViewById(R.id.pseudo2);
bAddData = (Button) findViewById(R.id.ajouter);
nicktobesaved = prefs.getString("nickname", "");
bAddData.setEnabled(prefs.getBoolean("isEnabled", true) ? true : false);
AddData();
} // End onCreate()
public void changeNickname(View v) {
// I delete this method who passed an intent
}
public void AddData() { // When user click on bAddData button
bAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
nicktobesaved = prefs.getString("nickname", "");
nicktobesaved = editPseudo.getText().toString();
// The following code is just for save data on my database SQLite
boolean isInserted = myDb.insertData(editNickname.getText().toString());
if (isInserted == true) {
Toast.makeText(ProfilActivity.this, "Data saved", Toast.LENGTH_LONG).show();
prefs.edit().putBoolean("isEnabled", false).apply();
bAddData.setEnabled(false);
} else {
Toast.makeText(ProfilActivity.this, "Data not saved", Toast.LENGTH_LONG).show();
}
}
}
);
}
}
Activity1 (Main activity):
public class MainActivity extends Activity {
TextView tv;
Button login;
public SharedPreferences prefs;
private String nickname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
nickname = prefs.getString("nickname", "");
login = (Button) findViewById(R.id.connexion);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentLogin = new Intent(MainActivity.this, ProfilActivity.class);
startActivity(intentLogin);
}
});
tv = (TextView)findViewById(R.id.pseudo);
if(nickname.equals("")) {
tv.setText("Welcome");
} else {
tv.setText("Welcome " + nickname);
}
}
}
Pseudo = nickname in french, Bienvenue = welcome in french, Thank u all for yo help, I hope u could understand my question. Pikkoro