Skip to main content

help with saveInstanceState on android project

No replies
cgodfrey987
Offline
Joined: 2012-11-30
Points: 0

I can't seem to make this activity save the edittexts inputs so that when I go to the next activity, and come back to this one, it brings up the info you had entered, rather than just showing the activity with empty edittext fields.

can someone see what I'm doing wrong or what I should change to hopefully make it work. The .java file is listed first. The corresponding xml file is listed second.

<br />
//import android.R.color;<br />
import android.os.Bundle;<br />
import android.app.Activity;<br />
import android.content.Intent;<br />
//import android.graphics.Color;<br />
import android.support.v4.app.NavUtils;<br />
import android.view.KeyEvent;<br />
import android.view.Menu;<br />
import android.view.MenuItem;<br />
import android.view.View;<br />
import android.view.View.OnKeyListener;<br />
import android.widget.Button;<br />
//import android.widget.EditText;<br />
import android.widget.EditText;</p>
<p>public class LoanOptionsActivity extends Activity {</p>
<p>	EditText twoIntRate, loanAmt, oneLoanTerm, oneIntRate, twoLoanTerm;<br />
	Button loanClearButton;</p>
<p>	@Override<br />
	protected void onCreate(Bundle savedInstanceState) {<br />
		super.onCreate(savedInstanceState);<br />
		setContentView(R.layout.activity_loan_options);</p>
<p>		 if (savedInstanceState != null)<br />
		  {<br />
		    String strValue = savedInstanceState.getString("savAmt");<br />
		    if (strValue != null)<br />
		    {<br />
		      loanAmt = (EditText)findViewById(R.id.editText1);<br />
		      loanAmt.setText(strValue);<br />
		    }</p>
<p>		    strValue = savedInstanceState.getString("savOneTerm");<br />
		    if (strValue != null)<br />
		    {<br />
		      oneLoanTerm = (EditText)findViewById(R.id.editText3);<br />
		      oneLoanTerm.setText(strValue);<br />
		    }</p>
<p>		    strValue = savedInstanceState.getString("savOneRate");<br />
		    if (strValue != null)<br />
		    {<br />
		      oneIntRate = (EditText)findViewById(R.id.editText5);<br />
		      oneIntRate.setText(strValue);<br />
		    }</p>
<p>		    strValue = savedInstanceState.getString("savTwoTerm");<br />
		    if (strValue != null)<br />
		    {<br />
		      twoLoanTerm = (EditText)findViewById(R.id.editText3);<br />
		      twoLoanTerm.setText(strValue);<br />
		    }</p>
<p>		    strValue = savedInstanceState.getString("savTwoRate");<br />
		    if (strValue != null)<br />
		    {<br />
		      twoIntRate = (EditText)findViewById(R.id.editText4);<br />
		      twoIntRate.setText(strValue);<br />
		    }</p>
<p>		  } </p>
<p>		loanAmt = (EditText) findViewById(R.id.editText1);<br />
		oneLoanTerm = (EditText) findViewById(R.id.editText3);<br />
		oneIntRate = (EditText) findViewById(R.id.editText5);<br />
		twoLoanTerm = (EditText) findViewById(R.id.editText2);<br />
		twoIntRate = (EditText) findViewById(R.id.editText4);</p>
<p>		//getWindow().getDecorView().setBackgroundColor(color.holo_green_light);<br />
		twoIntRate.setOnKeyListener(new OnKeyListener() {<br />
			public boolean onKey(View view, int keyCode, KeyEvent event) {<br />
				if(keyCode == KeyEvent.KEYCODE_ENTER){<br />
					loanComparison(view);<br />
					return true;<br />
				} else {<br />
					return false;<br />
				}</p>
<p>			}</p>
<p>		});</p>
<p>		loanClearButton = (Button) findViewById(R.id.button2);<br />
		loanClearButton.setOnClickListener(new View.OnClickListener() {<br />
			public void onClick(View v) {</p>
<p>				loanAmt.setText("");<br />
				oneLoanTerm.setText("");<br />
				oneIntRate.setText("");<br />
				twoLoanTerm.setText("");<br />
				twoIntRate.setText("");</p>
<p>			}</p>
<p>		});</p>
<p>	}</p>
<p>	@Override<br />
	public boolean onCreateOptionsMenu(Menu menu) {<br />
		// Inflate the menu; this adds items to the action bar if it is present.<br />
		getMenuInflater().inflate(R.menu.loan_options, menu);<br />
		return true;<br />
	}</p>
<p>	@Override<br />
	public boolean onOptionsItemSelected(MenuItem item) {<br />
		switch (item.getItemId()) {<br />
		case android.R.id.home:<br />
			// This ID represents the Home or Up button. In the case of this<br />
			// activity, the Up button is shown. Use NavUtils to allow users<br />
			// to navigate up one level in the application structure. For<br />
			// more details, see the Navigation pattern on Android Design:<br />
			//<br />
			// <a href="http://developer.android.com/design/patterns/navigation.html#up-vs-back" title="http://developer.android.com/design/patterns/navigation.html#up-vs-back">http://developer.android.com/design/patterns/navigation.html#up-vs-back</a><br />
			//<br />
			NavUtils.navigateUpFromSameTask(this);<br />
			return true;<br />
		}<br />
		return super.onOptionsItemSelected(item);<br />
	}</p>
<p>	@Override<br />
	public void onSaveInstanceState(Bundle savedInstanceState)<br />
	{<br />
	  // Store UI state to the savedInstanceState.<br />
	  // This bundle will be passed to onCreate on next call<br />
	  String savAmt, savOneTerm, savOneRate, savTwoTerm, savTwoRate;</p>
<p>	  savAmt = loanAmt.getText().toString();<br />
	  savOneTerm = oneLoanTerm.getText().toString();<br />
	  savOneRate = oneIntRate.getText().toString();<br />
	  savTwoTerm = twoLoanTerm.getText().toString();<br />
	  savTwoRate = twoIntRate.getText().toString();</p>
<p>	  savedInstanceState.putString("loanAmount", savAmt);<br />
	  savedInstanceState.putString("loanOneTerm", savOneTerm);<br />
	  savedInstanceState.putString("loanOneRate", savOneRate);<br />
	  savedInstanceState.putString("loanTwoTerm", savTwoTerm);<br />
	  savedInstanceState.putString("loanTwoRate", savTwoRate);</p>
<p>	  super.onSaveInstanceState(savedInstanceState);<br />
	} </p>
<p>	public void loanComparison(View view) {<br />
		Intent intent = new Intent(this, LoanCompareResults.class);<br />
		Bundle b = new Bundle();</p>
<p>		loanAmt = (EditText) findViewById(R.id.editText1);<br />
		oneLoanTerm = (EditText) findViewById(R.id.editText3);<br />
		oneIntRate = (EditText) findViewById(R.id.editText5);<br />
		twoLoanTerm = (EditText) findViewById(R.id.editText2);<br />
		twoIntRate = (EditText) findViewById(R.id.editText4);</p>
<p>		double dAmt, dOneRate, dOneTerm, dTwoRate, dTwoTerm;<br />
		String totalAmt, OneRate, OneTerm, TwoRate, TwoTerm;</p>
<p>		totalAmt = loanAmt.getText().toString();<br />
		OneTerm = oneLoanTerm.getText().toString();<br />
		OneRate = oneIntRate.getText().toString();<br />
		TwoTerm = twoLoanTerm.getText().toString();<br />
		TwoRate = twoIntRate.getText().toString();</p>
<p>	    if (!totalAmt.equals("") && !OneTerm.equals("") && !OneRate.equals(""))<br />
	    {<br />
	    	dAmt = Double.parseDouble(totalAmt);<br />
			dOneTerm = Double.parseDouble(OneTerm);<br />
	    	dOneRate = Double.parseDouble(OneRate);</p>
<p>	    	try{</p>
<p>				dTwoTerm = Double.parseDouble(TwoTerm);<br />
		    	dTwoRate = Double.parseDouble(TwoRate);<br />
			}<br />
			catch(Exception e){</p>
<p>				dTwoTerm = 0;<br />
		    	dTwoRate = 0;<br />
			}</p>
<p>			b.putDouble("recLoanAmt", dAmt);<br />
			b.putDouble("recOneTerm", dOneTerm);<br />
			b.putDouble("recOneIntRate", dOneRate);<br />
			b.putDouble("recTwoTerm", dTwoTerm);<br />
			b.putDouble("recTwoIntRate", dTwoRate);</p>
<p>			intent.putExtras(b);<br />
			startActivity(intent);</p>
<p>	    } //end of if stmt</p>
<p>	}	</p>
<p>}</p>
<p>

XML FILE CODE:

</p>
<p><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />
    xmlns:tools="http://schemas.android.com/tools"<br />
    android:layout_width="match_parent"<br />
    android:layout_height="match_parent"<br />
    android:paddingBottom="@dimen/activity_vertical_margin"<br />
    android:paddingLeft="@dimen/activity_horizontal_margin"<br />
    android:paddingRight="@dimen/activity_horizontal_margin"<br />
    android:paddingTop="@dimen/activity_vertical_margin"<br />
    tools:context=".LoanOptionsActivity" ></p>
<p>    <EditText<br />
        android:id="@+id/editText3"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_alignBaseline="@+id/editText2"<br />
        android:layout_alignBottom="@+id/editText2"<br />
        android:layout_alignLeft="@+id/textView4"<br />
        android:nextFocusDown="@+id/editText5"<br />
        android:ems="3"<br />
        android:inputType="numberDecimal" /></p>
<p>    <TextView<br />
        android:id="@+id/textView6"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_alignLeft="@+id/textView5"<br />
        android:layout_below="@+id/editText2"<br />
        android:layout_marginTop="14dp"<br />
        android:text="@string/lnRate"<br />
        android:textAppearance="?android:attr/textAppearanceMedium" /></p>
<p>    <EditText<br />
        android:id="@+id/editText5"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_alignBaseline="@+id/editText4"<br />
        android:layout_alignBottom="@+id/editText4"<br />
        android:layout_alignRight="@+id/textView4"<br />
        android:nextFocusDown="@+id/editText2"<br />
        android:ems="3"<br />
        android:inputType="numberDecimal"<br />
        android:maxLength="5" /></p>
<p>    <TextView<br />
        android:id="@+id/textView1"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_alignParentTop="true"<br />
        android:layout_centerHorizontal="true"<br />
        android:layout_marginTop="15dp"<br />
        android:text="@string/loanHeader"<br />
        android:textAppearance="?android:attr/textAppearanceLarge" /></p>
<p>    <EditText<br />
        android:id="@+id/editText4"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_alignLeft="@+id/editText2"<br />
        android:layout_below="@+id/editText2"<br />
        android:ems="3"<br />
        android:inputType="numberDecimal"<br />
        android:maxLength="5" /></p>
<p>    <TextView<br />
        android:id="@+id/textView5"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_alignParentLeft="true"<br />
        android:layout_below="@+id/textView3"<br />
        android:layout_marginTop="14dp"<br />
        android:text="@string/lnTerm"<br />
        android:textAppearance="?android:attr/textAppearanceMedium" /></p>
<p>    <TextView<br />
        android:id="@+id/textView3"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_alignParentRight="true"<br />
        android:layout_below="@+id/editText1"<br />
        android:layout_marginTop="37dp"<br />
        android:text="@string/lnTwoMobile"<br />
        android:textAppearance="?android:attr/textAppearanceMedium" /></p>
<p>    <TextView<br />
        android:id="@+id/textView4"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_alignBaseline="@+id/textView3"<br />
        android:layout_alignBottom="@+id/textView3"<br />
        android:layout_centerHorizontal="true"<br />
        android:text="@string/lnOne"<br />
        android:textAppearance="?android:attr/textAppearanceMedium" /></p>
<p>    <TextView<br />
        android:id="@+id/textView2"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_alignLeft="@+id/textView5"<br />
        android:layout_below="@+id/textView1"<br />
        android:layout_marginLeft="24dp"<br />
        android:layout_marginTop="18dp"<br />
        android:text="@string/loanCompTotal"<br />
        android:textAppearance="?android:attr/textAppearanceMedium" /></p>
<p>    <EditText<br />
        android:id="@+id/editText1"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_alignBaseline="@+id/textView2"<br />
        android:layout_alignBottom="@+id/textView2"<br />
        android:layout_marginLeft="17dp"<br />
        android:layout_toRightOf="@+id/textView2"<br />
        android:ems="5"<br />
        android:inputType="numberDecimal"<br />
        android:maxLength="10"<br />
        android:nextFocusDown="@+id/editText3" ></p>
<p>        <requestFocus /><br />
    </EditText></p>
<p>    <EditText<br />
        android:id="@+id/editText2"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_alignBottom="@+id/textView5"<br />
        android:layout_alignLeft="@+id/textView3"<br />
        android:ems="3"<br />
        android:inputType="numberDecimal"<br />
        android:maxLength="3"<br />
        android:nextFocusDown="@+id/editText4" /></p>
<p>    <Button<br />
        android:id="@+id/button1"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_below="@+id/editText5"<br />
        android:layout_marginTop="25dp"<br />
        android:layout_toRightOf="@+id/editText3"<br />
        android:onClick="loanComparison"<br />
        android:text="@string/lnOptionCalcButton" /></p>
<p>    <Button<br />
        android:id="@+id/button2"<br />
        android:layout_width="wrap_content"<br />
        android:layout_height="wrap_content"<br />
        android:layout_alignBaseline="@+id/button1"<br />
        android:layout_alignBottom="@+id/button1"<br />
        android:layout_toLeftOf="@+id/editText5"<br />
        android:onClick="clearInput"<br />
        android:text="@string/loanClearButton" /></p>
<p></RelativeLayout></p>
<p>