In some application it is needed to store activity state/data. If this data not store before activity finishes it loss the data. So before finishing activity you need to store data somewhere from where you can get it back when activity restarted.
There are many way to store activity state but here I am uses of SharePreferences. It uses because it easy to manage to user.
Here is complete example of save state of activity.
----------------------------------------------------------------------------------------------
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
public class SavingActivityStateActivity extends Activity {
@Override
protected void onDestroy() {
saveState();
super.onDestroy();
}
EditText etName;
EditText etAddress;
EditText etEmail;
EditText etPno;
RadioGroup rgGender;
CheckBox chbsingle;
Spinner spEdu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadUI(); // it load previously saved data if any data stored in preference.
}
// load data from saved preference.
private void loadUI() {
SharedPreferences savedPref=getPreferences(MODE_PRIVATE);
setEditText(R.id.etName, savedPref.getString("Name", ""));
setEditText(R.id.etAddress, savedPref.getString("Address", ""));
setEditText(R.id.etEmail, savedPref.getString("Email", ""));
setEditText(R.id.etPno, savedPref.getString("Pno", ""));
String gender=savedPref.getString("Gender", "Male");
rgGender=(RadioGroup) findViewById(R.id.rgGender);
if(gender.equals("Male"))
{
RadioButton rbMale=(RadioButton) findViewById(R.id.rbMale);
rbMale.setChecked(true);
}
else if(gender.equals("Female"))
{
RadioButton rbFemale=(RadioButton) findViewById(R.id.rbFemale);
rbFemale.setChecked(true);
}
boolean single=savedPref.getBoolean("Single", true);
chbsingle=(CheckBox) findViewById(R.id.chbSingle);
chbsingle.setChecked(single);
spEdu=(Spinner) findViewById(R.id.spEducation);
int edu=savedPref.getInt("Edu", 0);
spEdu.setSelection(edu);
}
@Override
protected void onPause() {
super.onPause();
// before finishing activity data of each field is store to preference using this method.
saveState();
}
// save data to preference
private void saveState() {
SharedPreferences savePref=getPreferences(MODE_PRIVATE);
// create preference
SharedPreferences.Editor editor=savePref.edit();
// create editor of preference to edit
// following code save each value of view to share preference
editor.putString("Name",getEditText(R.id.etName));
editor.putString("Address",getEditText(R.id.etAddress));
editor.putString("Email",getEditText(R.id.etEmail));
editor.putString("Pno",getEditText(R.id.etPno));
rgGender=(RadioGroup) findViewById(R.id.rgGender);
int selected=rgGender.getCheckedRadioButtonId();
RadioButton rbSelected=(RadioButton) findViewById(selected);
String gender=rbSelected.getText().toString();
editor.putString("Gender", gender);
chbsingle=(CheckBox) findViewById(R.id.chbSingle);
boolean single=chbsingle.isChecked();
editor.putBoolean("Single", single);
spEdu=(Spinner) findViewById(R.id.spEducation);
int edu=spEdu.getSelectedItemPosition();
editor.putInt("Edu", edu);
editor.commit();
}
private String getEditText(int id)
{
String text="";
EditText et=(EditText) findViewById(id);
text=et.getText().toString();
return text;
}
private void setEditText(int id, String text)
{
EditText et=(EditText) findViewById(id);
et.setText(text);
}
}
There are many way to store activity state but here I am uses of SharePreferences. It uses because it easy to manage to user.
Here is complete example of save state of activity.
----------------------------------------------------------------------------------------------
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
public class SavingActivityStateActivity extends Activity {
@Override
protected void onDestroy() {
saveState();
super.onDestroy();
}
EditText etName;
EditText etAddress;
EditText etEmail;
EditText etPno;
RadioGroup rgGender;
CheckBox chbsingle;
Spinner spEdu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadUI(); // it load previously saved data if any data stored in preference.
}
// load data from saved preference.
private void loadUI() {
SharedPreferences savedPref=getPreferences(MODE_PRIVATE);
setEditText(R.id.etName, savedPref.getString("Name", ""));
setEditText(R.id.etAddress, savedPref.getString("Address", ""));
setEditText(R.id.etEmail, savedPref.getString("Email", ""));
setEditText(R.id.etPno, savedPref.getString("Pno", ""));
String gender=savedPref.getString("Gender", "Male");
rgGender=(RadioGroup) findViewById(R.id.rgGender);
if(gender.equals("Male"))
{
RadioButton rbMale=(RadioButton) findViewById(R.id.rbMale);
rbMale.setChecked(true);
}
else if(gender.equals("Female"))
{
RadioButton rbFemale=(RadioButton) findViewById(R.id.rbFemale);
rbFemale.setChecked(true);
}
boolean single=savedPref.getBoolean("Single", true);
chbsingle=(CheckBox) findViewById(R.id.chbSingle);
chbsingle.setChecked(single);
spEdu=(Spinner) findViewById(R.id.spEducation);
int edu=savedPref.getInt("Edu", 0);
spEdu.setSelection(edu);
}
@Override
protected void onPause() {
super.onPause();
// before finishing activity data of each field is store to preference using this method.
saveState();
}
// save data to preference
private void saveState() {
SharedPreferences savePref=getPreferences(MODE_PRIVATE);
// create preference
SharedPreferences.Editor editor=savePref.edit();
// create editor of preference to edit
// following code save each value of view to share preference
editor.putString("Name",getEditText(R.id.etName));
editor.putString("Address",getEditText(R.id.etAddress));
editor.putString("Email",getEditText(R.id.etEmail));
editor.putString("Pno",getEditText(R.id.etPno));
rgGender=(RadioGroup) findViewById(R.id.rgGender);
int selected=rgGender.getCheckedRadioButtonId();
RadioButton rbSelected=(RadioButton) findViewById(selected);
String gender=rbSelected.getText().toString();
editor.putString("Gender", gender);
chbsingle=(CheckBox) findViewById(R.id.chbSingle);
boolean single=chbsingle.isChecked();
editor.putBoolean("Single", single);
spEdu=(Spinner) findViewById(R.id.spEducation);
int edu=spEdu.getSelectedItemPosition();
editor.putInt("Edu", edu);
editor.commit();
}
private String getEditText(int id)
{
String text="";
EditText et=(EditText) findViewById(id);
text=et.getText().toString();
return text;
}
private void setEditText(int id, String text)
{
EditText et=(EditText) findViewById(id);
et.setText(text);
}
}
Feeling superb to share the link to practice important android interview questions @ http://skillgun.com
ReplyDelete