Android:当应用程序关闭时保存实例状态

可能重复:
如何保存Android应用程序的状态?

我是Java和Android的新手,并构建了一些小型项目来学习。 我做了一个钱跟踪应用程序,允许用户输入值,它只是继续减去它。 一切工作正常,但我想要的值保存或缓存时,应用程序关闭并重新打开。 在阅读中,我发现OnPause可能会诀窍,但仍不能100%理解它。

任何人都可以推荐如何做到这一点,以及如何适用于我的代码?

非常感谢你的帮助!!

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());



              }
                    }
                                        });
    }
                }

请使用SharedPreferences将数据存储在私人应用程序存储中。 在像以下这样的onPause方法中实现SharedPrefernces

 @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();
    }
链接地址: http://www.djcxy.com/p/26185.html

上一篇: Android: Save an instance state when app is closed

下一篇: Save a Activity in Android When moved to another Activity