Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Save an instance state when app is closed [duplicate]

Possible Duplicate:
How do I save an Android application's state?

I am new to Java and Android and building some small projects to learn. I made a money tracking app which allows the user to input values and it simply continues to subtract it. Everything works fine but I wanted the values to save or cache when the app is closed and reopened. Reading, I found maybe a OnPause would do the trick, but still not 100% understanding it.

Can anyone recommend how to do this and how to apply to my code?

Thank you so much for your help!!

package ps.age.sl;


import java.text.NumberFormat;
import java.util.Locale;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;


public class MoneyTrackerActivity extends Activity {
    /** Called when the activity is first created. */
        ImageButton subtract;


        EditText startingmoney,submoney, endmoney, tracker;
        Locale currentLocale = Locale.getDefault();





    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

     //   startingmoney = (EditText) findViewById (R.id.firstmoney);
      //  submoney = (EditText) findViewById (R.id.submoney);
       // subtract = (ImageButton) findViewById (R.id.subbutton);
       // endmoney = (EditText) findViewById (R.id.endtv);
       // tracker = (EditText) findViewById (R.id.trackertv);

        startingmoney.setText("");
        submoney.setText("");
        endmoney.setText("");

subtract.setOnClickListener(new View.OnClickListener() {
         double currentValue=0;       
         double startValue=0;       
            public void onClick(View v) throws  NumberFormatException {


                    if (v == subtract)
                {
                NumberFormat currencyFormatter;
                currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);



                    String totalString;
                    String x = startingmoney.getText().toString();
                    String y = submoney.getText().toString ();
                            double total;
                        double xm = 0.00;
                        double ym =0.00;

                         try
                          {
                              xm = Double.parseDouble(x);
                          }
                         catch(NumberFormatException n)
                          {
                              xm = 0.00;
                          }
                         try
                         {
                                 ym = Double.parseDouble(y);
                         }
                        catch(NumberFormatException n)
                         {
                                ym = 0.00;
                         }

                   if(startValue!=xm){
                       startValue=xm;
                       currentValue=xm;
                   }

                   currentValue = currentValue -ym;


                    totalString = currencyFormatter.format(currentValue);
                    endmoney.setText(totalString);

                    tracker.setText("you have entered " + totalString +"\n" + tracker.getText().toString());



              }
                    }
                                        });
    }
                }
like image 333
user1218728 Avatar asked May 16 '12 12:05

user1218728


People also ask

How do you save an instance state?

The onSaveInstanceState() callback stores data needed to reload the state of a UI controller, such as an activity or a fragment, if the system destroys and later recreates that controller. To learn how to implement saved instance state, see Saving and restoring activity state in the Activity Lifecycle guide.

Which method is be called to allow the activity to save per instance state?

Save activity instance state in the onSaveInstanceState() method. Instance state data is stored as simple key/value pairs in a Bundle. Use the Bundle methods to put data into and get data back out of the bundle. Restore the instance state in onCreate(), which is the preferred way, or onRestoreInstanceState().

What allows you to properly restore a user state when an activity is restarted?

The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.

What is onPause method in Android?

onPause. Called when the Activity is still partially visible, but the user is probably navigating away from your Activity entirely (in which case onStop will be called next). For example, when the user taps the Home button, the system calls onPause and onStop in quick succession on your Activity .


1 Answers

Please use SharedPreferences to store the data in Private application Storage. Implementing SharedPrefernces in onPause method like below :

 @Override
    protected void onPause() 
    {
      super.onPause(); 
      // Store values between instances here
      SharedPreferences preferences = getSharedPreferences("sharedPrefs", 0);
      SharedPreferences.Editor editor = preferences.edit();
      editor.putString("CLASSLABEL", this.getLocalClassName()); // value to store
      editor.putString("STRINGLABEL",String1 );
      editor.putString("STRINGLABEL1",String2);
      // Commit to storage
      editor.commit();
    }
like image 67
Avadhani Y Avatar answered Nov 15 '22 18:11

Avadhani Y