简体   繁体   English

在方向改变时保留价值

[英]Preserving Value When Orientation Changes

I am developing a very basic Android App that adds a number in TextView whenever I hit the Button .我正在开发一个非常基本的 Android 应用程序,每当我点击Button时,它都会在TextView中添加一个数字。 The digit showing in TextView is also preserved when the orientation of the Mobile changes using the onSaveInstanceState() and onRestoreInstanceState() functions.当使用onSaveInstanceState()onRestoreInstanceState()函数更改 Mobile方向时, TextView 中显示的数字也会保留。

The problem is when orientation changes the value is preserved but when the Button is pressed once again after the changing of the orientation it again starts the counting from 0 rather then starting it from the preserved value.问题是当方向改变时,值被保留,但是当在改变方向后再次按下按钮时,它会再次从 0 开始计数,而不是从保留的值开始计数。

My code:我的代码:

public class MainActivity extends AppCompatActivity {

    TextView showValue;
    int counter=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showValue = (TextView) findViewById(R.id.CounterValue);
    }


    public void countIN(View view)
    {
        counter++;
        showValue.setText(Integer.toString(counter));
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("my_text", showValue.getText().toString());
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        showValue.setText(savedInstanceState.getString("my_text"));
    }
 }

Thank You for your response.谢谢您的答复。

You can save and get data using several methods 您可以使用多种方法保存和获取数据

First If your data is small, then you can use onSavedInstanceState and onRestoreInstanceState .. for details follow this answer or this one 首先如果您的数据很小,那么您可以使用onSavedInstanceStateonRestoreInstanceState ..详细信息请参阅此答案 答案

Second if you have too much data to store, then use ViewModel instead; 其次,如果您要存储太多数据,请改用ViewModel; this is a tutorial that you can start from. 是一个你可以从中开始的教程。

add

counter=Integer.parseInt(savedInstanceState.getString("my_text"));

inside onRestoreInstanceState method 在onRestoreInstanceState方法中

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    showValue.setText(savedInstanceState.getString("my_text"));
    counter=Integer.parseInt(savedInstanceState.getString("my_text"));
}

The problem is when orientation changes the value is preserved but when the Button is pressed once again after the changing of the orientation it again starts the counting from 0 rather then starting it from the preserved value 问题是当方向改变时保留了值,但是当改变方向后再次按下按钮时,它再次从0开始计数而不是从保留值开始计数

That's because when you restore the value of counter after configuration change, you set it as a text on a TextView but you don't set it to counter variable. 这是因为在配置更改后恢复counter的值时,将其设置为TextView上的文本,但不要将其设置为计数器变量。 So when button is clicked, it takes the previous value of counter which is zero after each configuration change , it increments it and sets it as text on TextView . 因此,当单击按钮时,它会获取counter的先前值, zero在每次配置更改后zero ,它会将其递增并在TextView上将其设置为文本。

Solution

Inside onRestoreInstanceState method, add the following statement onRestoreInstanceState方法中,添加以下语句

counter = Integer.parseInt(savedInstanceState.getString("my_text"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM