在java代码中使用savedInstanceState来保存可重用的值

时间:2017-12-15 22:13:51

标签: java android

我正在开发一个Android应用程序,并希望使用savedInstanceState保存rand2(Double类型)值,这样我可以在重新打开app时使用rand2值但是在检索rand2值时它总是为NULL,值不是保存或者值不是检索。为什么会发生这种情况以及如何保存rand2值以便我可以在重新打开应用程序时重复使用它?

public class MainActivity extends AppCompatActivity {


    double rand2;
    private boolean started = false;
    private Handler handler = new Handler();
    public Runnable runnable = new Runnable() {
        @Override
        public void run() {

            double rand1 = Math.random() * 5;

            rand2 = rand2 + rand1 * 0.04;

            DecimalFormat df = new DecimalFormat("0.00");

            String message1 = "" + df.format(rand1);

            DecimalFormat dff = new DecimalFormat("000000.00");

            String message2 = "" + dff.format(rand2);

            displayRate(message1);

            displaySatoshi(message2);


            if (started) {
                start(started);
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // recovering the instance state
        // Restore UI state from the savedInstanceState.
        // This bundle has also been passed to onCreate.
        if (savedInstanceState != null) {
            rand2 = savedInstanceState.getDouble("abc");
        } else {
            rand2 = 0.00;
        }


        setContentView(R.layout.activity_main);


        // Find the View that shows the numbers category
        TextView numbers = (TextView) findViewById(withdraw);
        // Set a click listener on that View
        numbers.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the numbers View is clicked on.
            @Override
            public void onClick(View view) {
                Intent numbersIntent = new Intent(MainActivity.this, Withdraw.class);
                startActivity(numbersIntent);
            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {

        savedInstanceState.putDouble("abc", rand2);


        // call superclass to save any view hierarchy
        super.onSaveInstanceState(savedInstanceState);

    }

    public void Start(View v) {


        ToggleButton starStopTogglrButton = (ToggleButton) findViewById(R.id.start_stop);
        boolean hasStartStop = starStopTogglrButton.isChecked();

        if (hasStartStop) {

            start(hasStartStop);

        } else {
            stop(hasStartStop);
        }


    }

    public void stop(Boolean hasStartStop) {


        //Checking start or stop
        started = hasStartStop;
        handler.removeCallbacks(runnable);
    }

    public void start(Boolean hasStartStop) {
        started = hasStartStop;
        handler.postDelayed(runnable, 1000);
    }


    private void displayRate(String message1) {
        TextView orderSummaryTextView = (TextView) findViewById(R.id.rate);

        orderSummaryTextView.setText(message1);
    }

    private void displaySatoshi(String message2) {
        TextView orderSummaryView = (TextView) findViewById(R.id.satoshis);

        orderSummaryView.setText(message2);


    }


}

1 个答案:

答案 0 :(得分:0)

应用程序关闭时会调用

onSaveInstanceState,但仅在应用程序完成后启动时调用onCreate。记住活动生命周期:

enter link description here

因此,在关闭时调用onSaveInstanceState并且仅在(重新)创建活动时调用onCreate,因为它在那时没有添加,所以它为null。

您正在寻找onRestoreInstanceState。覆盖该方法并获取变量并从那里分配它。

请记住,如果活动被完全销毁,则使用savedInstanceState 不会保存数据。对于持久数据存储,请使用sharedprefs,files或SQL