I'm working on an android app that contain sqlite table , and the activity that display this activity contains setting menu and it has an option to choose how sort the table either by name, date or id and everything is working fine but when I choose to sort by name and then leave the activity it doesn't save the state that I chose (the sorting option) How can I save the state of the activity after choosing a menuItem , Or is there an example that could help me with my problem thanx
Asked
Active
Viewed 60 times
-1
-
1see this post https://stackoverflow.com/questions/151777/how-to-save-an-android-activity-state-using-save-instance-state – Anice Jahanjoo Jun 24 '19 at 10:45
-
Possible duplicate of [How to save an Android Activity state using save instance state?](https://stackoverflow.com/questions/151777/how-to-save-an-android-activity-state-using-save-instance-state) – m0skit0 Jun 24 '19 at 12:01
1 Answers
-1
you can use shared preferences to store and retrieve your settings by saving the value when taping on the menuItem
SharedPreferences.Editor editor = getSharedPreferences("saving", MODE_PRIVATE).edit();
editor.putString("sort", "by name");
editor.apply();
and retrieve it when activity start in onCreate
SharedPreferences prefs = getSharedPreferences("saving", MODE_PRIVATE);
String sorting= prefs.getString("sort", "No name defined");//"No name defined" is the default value.
and then check
if (sorting.equals("by name")){
// do something
}
i used Elenasys answer Android Shared preferences example

Ibrahim Atef Elmasry
- 119
- 7