I am new to android programming and I created a simple calculator. I want the user to be able to pause the application without losing the numbers entered in the two EditText boxes. I am trying to use shared preferences but for some reason my app keeps crashing. How can I fix this?
package com.example.simplecalculator;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.example.simplecalculator.*;
public class MainActivity extends Activity {
SharedPreferences mySharedPreferences;
SharedPreferences.Editor editor;
public static final String MYPREFERENCES = "MyPreferences_001";
float answer;
final EditText numberone = (EditText)findViewById(R.id.number1);
final EditText numbertwo = (EditText)findViewById(R.id.number2);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySharedPreferences = getSharedPreferences(MYPREFERENCES, 0);
String number1 = mySharedPreferences.getString("number1", null);
String number2 = mySharedPreferences.getString("number2", null);
numberone.setText(number1);
numbertwo.setText(number2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void Addition(View view) {
answer = Float.parseFloat(numberone.getText().toString()) + Float.parseFloat(numbertwo.getText().toString());;
TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your sum is " + answer);
}
public void Subtraction(View view) {
answer = Float.parseFloat(numberone.getText().toString()) - Float.parseFloat(numbertwo.getText().toString());;
TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your difference is " + answer);
}
public void Multiplication(View view) {
answer = Float.parseFloat(numberone.getText().toString()) * Float.parseFloat(numbertwo.getText().toString());;
TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your product is " + answer);
}
public void Division(View view) {
answer = Float.parseFloat(numberone.getText().toString()) / Float.parseFloat(numbertwo.getText().toString());;
TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your quotient is " + answer);
}
public void Power(View view) {
answer = (float) Math.pow(Float.parseFloat(numberone.getText().toString()), Float.parseFloat(numbertwo.getText().toString()));
TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your answer is " + answer);
}
public void Root(View view) {
answer = (float) Math.sqrt(Float.parseFloat(numberone.getText().toString()));
TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your answer is " + answer);
}
public void onPause(){
mySharedPreferences = getSharedPreferences(MYPREFERENCES, Activity.MODE_PRIVATE);
editor = mySharedPreferences.edit();
editor.putString("number1", numberone.getText().toString());
editor.putString("number2", numbertwo.getText().toString());
editor.commit();
}
}
final EditText numberone = (EditText)findViewById(R.id.number1);
final EditText numbertwo = (EditText)findViewById(R.id.number2);
Keep that inside onCreate(), after setContentView()
Also,
What happens the first time, when nothing is stored in sharedPref,
String number1 = mySharedPreferences.getString("number1", null);
String number2 = mySharedPreferences.getString("number2", null);
return some value then instead of "null" .
If using SharedPref pretty often for a Calculator app, i suggest making a function for get and put the sharedPref..
Something like:
public static void saveDataToPreferences(String key,
String value) {
SharedPreferences prefs = context.getSharedPreferences("your package name",
Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public static String getDataFromPreferences(String key) {
SharedPreferences prefs = context.getSharedPreferences("your package name",
Context.MODE_PRIVATE);
return prefs.getString(key, Constants.BLANK);
}
If the functions are inside your activity, the context for that activity can be declared globally. Else pass that in the arguments.
Your application is crashing because you are trying to initializing your view before setting any layout to your activity. Now, initialize the as follows...your problem will be solved.
EditText numberone;
EditText numbertwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberone = (EditText)findViewById(R.id.number1);
numbertwo = (EditText)findViewById(R.id.number2);
}
Try this..
You have initilize your EditText before onCreate do that inside onCreate
float answer;
final EditText numberone;
final EditText numbertwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberone = (EditText)findViewById(R.id.number1);
numbertwo = (EditText)findViewById(R.id.number2);
You are getting NULL Pointer exception most probably. Its because at first when your application sharedprefrences doesn't have any thing, you tend to access it and get a value from it and when its not present you set value of string NULL. When you try to show that value which is NULL on the screen, it crashes.
Do this:
String number1 = mySharedPreferences.getString("number1", "");
String number2 = mySharedPreferences.getString("number2", "");
I think you need to use onSaveInstanceState() and onRestoreInstanceState() for this purpose.
It's used when the Activity is forcefully terminated byactivity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish() or the OS (ex: when your Activity is in the background and another task needs resources) . When this happens, onSaveInstanceState(Bundle outstate) will be called and it's up to your app to add any state data you want to save in outstate.
When the user resumes your Activity, onCreate(Bundle savedInstanceState) gets called and savedInstanceState will be non-null if your Activity was terminated in a scenario described above. Your app can then grab the data from savedInstanceState and regenerate your Activity's state to how it was when the user last saw it.
Basically in onCreate, when savedInstanceState is null, then it means this is a 'fresh' launch of your Activity. And when it's non-null (if your app saved the data in onSaveInstanceState(...), it means the Activity state needs to be recreated.
Here is much more detail about it
Related
I'm trying to save textview from getIntent String. When I close the application, then open again, textview is null. I have used many methods like onSaveInstance, SharedPreference but all failed.
Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
jamShubuh = findViewById(R.id.jamShubuhTextView);
jamDzuhur = findViewById(R.id.jamDzuhurTextView);
jamAshar = findViewById(R.id.jamAsharTextView);
getShubuh = getIntent().getStringExtra("getShubuh");
getDzuhur = getIntent().getStringExtra("getDzuhur");
getAshar = getIntent().getStringExtra("getAshar");
jamShubuh.setText(getShubuh);
jamDzuhur.setText(getDzuhur);
jamAshar.setText(getAshar);
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("shubuh", getShubuh);
editor.putString("dzuhur", getDzuhur);
editor.putString("ashar", getAshar);
retriveData();
private void retriveData() {
SharedPreferences getData = getPreferences(Context.MODE_PRIVATE);
getData.getString("shubuh", null);
getData.getString("dzuhur", null);
getData.getString("ashar", null);
}
To use the shared preferences, I suggest you to make certain class to handle it. Maybe like this.
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class MyPrefManager {
SharedPreferences sp;
SharedPreferences.Editor editor;
public void setPref(Context context, String key, String value){
sp = PreferenceManager.getDefaultSharedPreferences(context);
editor = sp.edit();
editor.putString(key, value);
editor.commit();
}
public String getPref(Context context, String key){
sp = PreferenceManager.getDefaultSharedPreferences(context);
value = null;
String X = sp.getString(key, value);
return X;
}
}
Then use setPref method to set the value and getPref method to get the value. Assume now you are in MainActivity and you want to save values of getShubuh, getDzuhur, and getAshar to Preference Manager, you can do something like this :
MyPrefManager MPM = new MyPrefManager();
MPM.setPref(MainActivity.this,"keyShubuh",getShubuh);
MPM.setPref(MainActivity.this,"keyDzuhur",getDzuhur);
MPM.setPref(MainActivity.this,"keyAshar",getAshar);
And to get the value of keyShubuh, keyDzuhur, and keyAshar wherever you want, you can do something like this.
MyPrefManager MPM = new MyPrefManager();
MPM.getPref(AnyActivity.this,"keyShubuh");
MPM.getPref(AnyActivity.this,"keyDzuhur");
MPM.getPref(AnyActivity.this,"keyAshar");
Hope it can help.
To save text state
//Saving The Text Entered by User
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
editText = findViewById(R.id.editText);// getting the reference of editext from xml
CharSequence text = editText.getText();// getting text u entered in edittext
outState.putCharSequence("savedText", text);// saved that text in bundle object i.e. outState
}
and restore saved text
//Restoring the State
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
editText = findViewById(R.id.editText);// getting the reference of textview from xml
CharSequence savedText =
savedInstanceState.getCharSequence("savedText");// getting the text of editext
editText.setText(savedText);// set the text that is retrieved from bundle object
}
instead of EditText you can use TextView.
My Simple Interest calculator is crashing with a null pointer exception error, not sure what the problem is, there are no errors in the IDE before I complile, this is new to me. Here is my code and logcat: edit: Icouldn't post a logcat as the editor thinks it's code and I can't get it to format correctly
Code:
package com.codeherenow.sicalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.SeekBar;
public class SICalculatorActivity extends Activity
implements SeekBar.OnSeekBarChangeListener, View.OnClickListener{
private int years;
private TextView YT;
private SeekBar bar;
private EditText principal = (EditText) findViewById(R.id.PA_field);
private EditText interest = (EditText) findViewById(R.id.IR_field);
public EditText pvalue;
public EditText ivalue;
private double mPvalue = 0;
private double mIvalue = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sicalculator);
bar = (SeekBar)findViewById(R.id.seekBar);
bar.setOnSeekBarChangeListener(this);
pvalue = (EditText) principal.getText();
ivalue = (EditText) interest.getText();
String s = principal.getText().toString();
mPvalue = Double.parseDouble(s);
String s2 = interest.getText().toString();
mIvalue = Double.parseDouble(s2);
}
#Override
public void onProgressChanged (SeekBar seekBar,int i, boolean b){
years = i;
YT = (TextView) findViewById(R.id.Years);
YT.setText(years + " Year(s)");
}
#Override
public void onStartTrackingTouch (SeekBar seekBar){
}
#Override
public void onStopTrackingTouch (SeekBar seekBar){
}
#Override
public void onClick(View view) {
TextView fTextView = (TextView) findViewById(R.id.finalText);
double finValue = mPvalue * (mIvalue/100) * years;
fTextView.setText("The interest for " + pvalue + "at a rate of " + ivalue + "for " + years + "year(s) is " + finValue);
}
}
You can't instantiate view variables by calling findViewById as you're declaring them. You have to declare them first and then instantiate either in onCreate or some method invoked after the Activity is bound with the view. Make sure to do it after setContentView(R.layout.sicalculator);
Okay, after seeing your layout, stacktrace and reading more in to your code, I saw that there are fundamental issues which ought to give you more crashes, so let's fix them.
First, pvalue and ivalue variables are unnecessary! Remove them.
Related: You cannot assign an Editable to an EditText. So this line is invalid ivalue = (EditText) interest.getText(); Because getText() returns an Editable. But these are all redundant and unnecessary anyways.
Second, in onCreate method, let's just initialize views and not try to get values and parse them yet; the user (or you) haven't interacted nor entered any values there yet; so trying to parse null values in to Doubles will crash your app.
Your onCreate method should look something like this. (Note that I'm also initializing your button and setting the click listener here).
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sicalculator);
principal = (EditText) findViewById(R.id.PA_field);
interest = (EditText) findViewById(R.id.IR_field);
bar = (SeekBar) findViewById(R.id.seekBar);
bar.setOnSeekBarChangeListener(this);
calcBtn = (Button)findViewById(R.id.calc_btn);
calcBtn.setOnClickListener(this);
}
Now, you should get the values and parse them in your onClick listener - only when the user has entered values and clicked on the Calculate button, so your onClick() method should look something like this:
#Override
public void onClick(View view)
{
TextView fTextView = (TextView) findViewById(R.id.finalText);
mPvalue = Double.valueOf(principal.getText().toString());
mIvalue = Double.valueOf(interest.getText().toString());
double finValue = mPvalue * (mIvalue / 100) * years;
fTextView.setText("The interest for " + mPvalue + "at a rate of " + mIvalue + "for " + years + "year(s) is " + finValue);
}
And that should clear the logic and order or declaring, initializing, retrieving values from variables for you. I hope this explains how Java and Android basics work.
Oh by the way, I actually ran the code and it's running on my phone so this isn't just off the top of my head. If this helped you, please accept the answer.
Best wishes,
I am new to Android development. I am trying to call a method of one of my classes when a button on my main activity is pressed.
On my Main Activity I have this button:
public void buttonTest(){
Button b = (Button) findViewById(R.id.test);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String s = "changeText:myText";
Intent in = new Intent(PlusActivity.this, Test.class);
in.putExtra("method",s);
startActivity(in);
}
});
}
And here is is the class (without imports) which that intent above is calling to.
public class Test extends Activity {
static String text = "test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
TextView mTextView = (TextView) findViewById(R.id.textView);
mTextView.setText(text);
}
public void changeText(String s){
this.text = s;
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String[] array = intent.getStringExtra("method").split(":");
if(array[0].equals("changeText")){
changeText(array[1]);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.test, menu);
return true;
}
}
Basically I want to know if it is possible to change the value of that String text, before onCreate(). Basically each button will have a correspondent text, and I want to be able to modify that text based on which button.
If it is, what should I do/change?
Thanks in advance.
The right way to do it is to send the string you want it to be as an extra in the intent, and to read the extra from the intent and assign it to that variable in the onCreate function.
Use SharedPreference. Save in OnCLick of first class and retrieve in OnCreate of second class.
Initialization
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
Storing Data
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long
editor.commit(); // commit changes
Retrieving Data
// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean
Deleting Data
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes
Clearing Storage
editor.clear();
editor.commit(); // commit changes
String text;
if (savedInstanceState == null) {
extras = getIntent().getExtras();
if(extras == null) {
text= null;
} else {
text= extras.getString("your default string message");
}
} else {
String s = "your default string message";
text= (String) savedInstanceState.getSerializable(s);
}
I would like to pass a value that I get from an EditText to two activities with a click of a button. I managed to pass the value to one activity, displaying it in textview and i would like to do the same to another activity but I'm not sure if switch case is the right method to use since there's only one button.
This is the codes for the first activity:
public class ProfInfo1 extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profinfo1);
final EditText et0 = (EditText) findViewById (R.id.et_name);
final EditText et1 = (EditText) findViewById (R.id.et_age);
final EditText et2 = (EditText) findViewById (R.id.et_height);
final EditText et3 = (EditText) findViewById (R.id.et_weight);
Button back = (Button) findViewById(R.id.btn_back1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent a = new Intent(ProfInfo1.this, WelcomeActivity.class);
startActivity(a);
}
});
Button next = (Button) findViewById(R.id.btn_next1);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(ProfInfo1.this, ViewProf1.class);
intent.putExtra("name", et0.getText().toString());
intent.putExtra("age", et1.getText().toString());
intent.putExtra("height", et2.getText().toString());
intent.putExtra("weight", et3.getText().toString());
startActivity(intent);
}
});
}
}
This is the second activity where the values are displayed:
public class ViewProf1 extends Activity {
EditText age, height, weight;
String a, h, w;
Float age1, height1, weight1;
float bmi, cal;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profinfo1view);
TextView nameView = (TextView) findViewById (R.id.view_name);
nameView.setText("Hello " + getIntent().getExtras().getString("name") + ",");
TextView ageView = (TextView) findViewById (R.id.view_age);
ageView.setText("Age: " + getIntent().getExtras().getString("age"));
TextView heightView = (TextView) findViewById (R.id.view_height);
heightView.setText("Your current height is " + getIntent().getExtras().getString("height") + " m.");
TextView weightView = (TextView) findViewById (R.id.view_weight);
weightView.setText("Your current weight is " + getIntent().getExtras().getString("weight") + " kg.");
a = getIntent().getExtras().getString("age");
h = getIntent().getExtras().getString("height");
w = getIntent().getExtras().getString("weight");
age1 = Float.valueOf(a).floatValue();
height1 = Float.valueOf(h).floatValue();
weight1 = Float.valueOf(w).floatValue();
//bmi calculation
bmi = (float) (weight1/Math.pow(height1, 2));
TextView showresult = (TextView) findViewById (R.id.view_bmi);
showresult.setText("Your BMI is " + Float.valueOf(bmi) + ".");
//cal intake calculation
cal = (float) (((655 + (9.6 * weight1) + (1.8 * height1) - (4.7 * age1)) * 1.2) - 550);
TextView showcal = (TextView) findViewById (R.id.view_cal);
showcal.setText("Your ideal daily calories intake is " + Float.valueOf(cal) + ".");
//next button
Button next2 = (Button) findViewById (R.id.btn_next2);
next2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent a = new Intent(ViewProf1.this, FoodDiary.class);
startActivity(a);
}
});
//back button
Button back2 = (Button) findViewById (R.id.btn_back2);
back2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent a = new Intent(ViewProf1.this, ProfInfo1.class);
startActivity(a);
}
});
}
the third activity:
public class CompareWeight extends Activity {
EditText weight;
String w;
Float weight1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.compareweight);
TextView weightView = (TextView) findViewById (R.id.test);
weightView.setText("Your previous weight is " + getIntent().getExtras().getString("weight") + " kg.");
w = getIntent().getExtras().getString("weight");
weight1 = Float.valueOf(w).floatValue();
}
}
I only want to pass the weight value to the third activity.
you can pass values to only one activity at a time.but if you want to pass weight value to third activity,then you can set it's value in first activity and retrieve in third activity.
So basically I mean
Put values that you want to pass in SecondActivity with the help of putExtra.
Set the sharedPrefernce variable.
startActivity(intent);
You can save the value in Shared Preferences and update the value o Button Click .Thus values will be available to other activities .
Use SharedPreferences in onPause for current Activity as below and send the data to other activities as below:
#Override
protected void onPause()
{
super.onPause();
SharedPreferences preferences = getSharedPreferences("sharedPrefs", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("SearchText",edt.getText().toString());
editor.commit();
}
and in other two Activities, get the data from Shared Preferences as below:
SharedPreferences preferences = getSharedPreferences("sharedPrefs", 0);
String edtText = preferences.getString("SearchText","");
You can also get your string in the second activity like this :
Bundle extras = getIntent().getExtras();
if(extras != null) {
item = extras.getString("myitempassed");
}
I don't see where in the code you are calling the third activity (CompareWeight).
When you start the CompareWeight activity, you can just pass in the weight value using intent extras like you did for the second activity.
If you don't have the weight value at that point, then you'll need to save it - #Avadhani Y has a good suggestion for using SharedPreferences. Or you can use a SQLite DB to store your values.
By use of PutExtra before startActivity.
It is because in your Second Activity you just calling the Intent without passing any values in it,
In Second Activity
Replace the Intent
Intent a = new Intent(ViewProf1.this, FoodDiary.class);
startActivity(a);
With this
Intent a = new Intent(ViewProf1.this, FoodDiary.class);
a.putExtra("weight", weight1);
startActivity(a);
I'm trying to store a user entry into an EditText field however when I exit the application it does not appear.
So for example a user types in his/her name and then exits the application. When the user returns and launches the Application the users name appears in the EditText field. However I can't get this to work. I believe its to do with sharedPreferences but I'm not sure where I'm going wrong.
I'm quite new to android and java so finding this quite difficult. Any help would be much appreciated.
public class MainActivity extends Activity {
public final static String EXTRA_FROM = "com.example.assignment1.FROM";
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
EditText emailFrom =(EditText) findViewById(R.id.editEmailFrom);
String from = emailFrom.getText().toString();
outState.putString(EXTRA_FROM, from);
}
#Override
protected void onRestoreInstanceState(Bundle savedState)
{
EditText emailFrom =(EditText) findViewById(R.id.editEmailFrom);
String from = savedState.getString(EXTRA_FROM);
emailFrom.setText(from);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void emailSend (View sendButton)
{
Intent intent = new Intent(this,DisplayEmailActivity.class);
EditText emailFrom =(EditText) findViewById(R.id.editEmailFrom);
String from = emailFrom.getText().toString();
intent.putExtra(EXTRA_FROM,from);
SharedPreferences saveFrom = getSharedPreferences(EXTRA_FROM, MODE_PRIVATE);
Editor editor = saveFrom.edit();
editor.putString(EXTRA_FROM, from);
editor.commit();
String storedfrom = saveFrom.getString(EXTRA_FROM, from);
emailFrom.setText(storedfrom);
startActivity(intent);
}
Second Activity
public class DisplayEmailActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_email);
Intent intent = getIntent();
String from = intent.getStringExtra(MainActivity.EXTRA_FROM);
TextView textFrom =(TextView)findViewById(R.id.displayEmailFrom);
textFrom.setText(from);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_display_email, menu);
return true;
}
Ok, so after you store value in your EditText emailFrom, you have to save it in SharedPreferences like this:
String from = emailFrom.getText().toString(); // Getting String value from EditText and storing it in "from" String
SharedPreferences settings = getSharedPreferences("MyPreferencesFile", 0); // Opening SharedPreferences
SharedPreferences.Editor editor = settings.edit(); // Opening editor for SharedPreferences
editor.putString("exampleName", from); // You are putting here a String "from" and give it a "exampleName" name. Later you will use this name to restore data.
And then when you launch your application, you need to load data from SharedPreferences:
SharedPreferences settings = getSharedPreferences("MyPreferencesFile", 0); // Again opening SharedPreferences
String from = settings.getString("exampleName", ""); // The second argument is the default value. The default value will be set if there wasn't saved any data with "exampleName" name
if(from != "") // If "from" is not empty, it means that the data was stored in SharedPreferences
emailFrom.setText(from); // Setting text in your EditText
You are using onSaveInstanceState() and onRestoreInstanceState() to save and restore the content of the EditText. However, this methods are called only when the application is being terminated by the OS to be immediatlly recreated (i.e. device rotation).
If you want to preserve values when the application is terminated by the user, you need to store it somewhere, a file, a database or in your case I would suggest SharedPreferences.
I've posted an answer to a similar question in: SharePreferences
Good luck.