java - 如何让应用程序不自行重置?

标签 java android eclipse counter reset

我制作了一个从 100000 开始倒数的计数器应用程序。每次我将设备旋转 90° 或停止时,它都会再次从 100000 开始。如何让我的应用停止自行重置?

代码:

public class SecondActivity extends Activity {

    int counter = 1000001;

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

    public void onClick(View v){}

    public void button1 (View v){
        counter--;
        final Button button2 = (Button) findViewById(R.id.button2);
        button2.setText("" + counter); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }

}

最佳答案

Android 在方向改变时销毁并重建当前 Activity。您必须通过实现 ActivityonSaveInstanceState()onRestoreInstanceState() 方法来保存 Activity 的状态。

参见this article有关详细信息,请访问developer.android.com。

关于java - 如何让应用程序不自行重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19609445/

相关文章:

java - Android - 在应用程序中显示不同的视频

java - MySQL 中的文本版本控制

java - 带有 ADT 插件的 Eclipse - 布局编辑器不显示内容(卡在 "Loading editor"上)

java - 如何在eclipse中添加 "create on"注释?

android - 如何在用户触摸屏幕时停止 OnTouchListener? <机器人>

android - flutter : Publish App Privately

eclipse - java.lang.IllegalArgumentException : Document base [. ..] 不存在或不是可读目录

java - FedEx 跟踪 API 错误 9040 - Java Web 服务

java - 线程 "main"org.openqa.selenium.NoSuchElementException : An element could not be located on the page using the given search parameters 中出现异常

android - Firebase token 刷新