public
interface
Storage {
public
int
getInt(key, defaultValue);
public
void
setInt(key, value);
}
public
class
PrefStorage
implements
Storage {
private
final
SharedPreferences sharedPref;
public
PrefStorage(Context context) {
sharedPref = context.getPreferences(Context.MODE_PRIVATE);
}
public
int
getInt(key, defaultValue) {
return
sharedPref.get(key, defaultValue);
}
public
void
setInt(key, value) {
sharedPref.edit()
.putInt(key, value)
.apply();
}
}
public
interface
Settings {
public
int
getLastSelectedTextPosition();
public
int
setLastSelectedTextPosition(
int
value);
}
public
class
AppSettings
implements
Settings {
private
static
final
YOU_KEY_NAME =
"your_key_name"
private
final
Storage storage;
public
AppSettings(Storage storage) {
this
.storage = storage;
}
private
int
public
int
getLastSelectedTextPosition() {
return
storage.getInt(YOU_KEY_NAME,
0
);
}
public
int
setLastSelectedTextPosition(
int
value) {
storage.putInt(YOU_KEY_NAME, value);
}
}
private
Settings settings;
public
void
onCrate(...) {
super
.onCreate(...);
initViewState();
}
private
void
initViewState() {
settings =
new
AppSettings(
new
PrefStorage(geApplicationContext());
val lastPosition= settings.getLastSelectedTextPosition();
button.setText(getButtonText(lastPosition));
}