Save Variables
public class MainActivity extends AppCompatActivity {
private Button saveButton;
private Switch switch1;
private TextView textView;
public static final String SHARED_PREFS = "sharedPrefs";
public static final String TIMER = "timer";
int selectedTimerItem = 0;
public static final String SWITCH1 = "switch1";
private boolean switchOnOff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
saveButton = (Button) findViewById(R.id.save_button);
switch1 = (Switch) findViewById(R.id.switch1);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveData();
}
});
loadData();
textView.setText(text);
switch1.setChecked(switchOnOff);
}
public void saveData() {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(TIMER, selectedTimerItem);
editor.putBoolean(SWITCH1, switch1.isChecked());
editor.apply();
// Toast.makeText(this,"set time: "+ mTimeLeftInMillis, Toast.LENGTH_SHORT).show();
}
public void loadData() {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
selectedTimerItem = sharedPreferences.getInt(TIMER, 0);
switchOnOff = sharedPreferences.getBoolean(SWITCH1, false);
}
}
0 Comments