简体   繁体   English

Android onSaveInstance State不起作用(对我来说)

[英]Android onSaveInstance State not working (for me)

I have an application that acts like a clapperboard, in which I use a variable i going to i++ every millisecond (I need milliseconds to display frames per second, and the chronometer updates only once per second), then I display it in the format HH:MM:SS:FF. 我有一个类似于clapperboard的应用程序,在其中我使用一个变量,我每毫秒就去一次i ++(我需要毫秒来每秒显示帧,而天文钟每秒只更新一次),然后以HH格式显示:MM:SS:FF。 I also have a quit button which goes by 我也有一个退出按钮

if (item.getTitle() == "Quit") {
Process.killProcess(id);
}

The problem is that I want the app to remember the value of i when I press quit, so the timer would start at the same point it was before quitting it if I start it again. 问题是我希望应用程序在按quit时记住i的值,因此如果我再次启动它,计时器将在退出它之前的同一时间启动。 I tried 我试过了

public void onSaveInstanceState(Bundle outState) {
outState.putLong(MILLISECONDS, i);
super.onSaveInstanceState(outState);
}

then calling it by 然后打电话给

public void onStart(Bundle savedInstanceState) {
super.onStart();
i = savedInstanceState.getLong(MILLISECONDS);
}

and

public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
i = savedInstanceState.getLong(MILLISECONDS);
}

but it doesn't work. 但这不起作用。 Also if I go with 另外如果我去

onCreate(Bundle savedInstanceState) {

...

i = savedInstanceState.getLong(MILLISECONDS);

...

}

the app force closes. 应用程序强制关闭。 Any idea of what I'm doing wrong, please? 请问我做错了什么吗? Thank you very much. 非常感谢你。

You are not really doing it the Android way :-) Don't kill your app, but go back to home. 您并不是真的以Android方式进行操作:-)不要杀死您的应用程序,而要回到家。 In that case, saveinstancestate will be called. 在这种情况下,将调用saveinstancestate。

It would help if you added some more details to your question: 如果您在问题中添加了更多详细信息,将会有所帮助:

"It doesn't work" -> how exactly does it not work? “它不起作用”->它到底怎么不起作用? Is savedInstanceState empty? saveInstanceState是否为空? Does it through an exception? 是否通过例外?
"the app force closes" -> can you provide the exact exception that is thrown, or the logcat text around the crash? “应用程序强制关闭”->您能提供抛出的确切异常,还是崩溃附近的logcat文本?

That being said, this section of documentation on saving persistant state might be helpful. 话虽如此,有关保存持久性状态的文档这一部分可能会有所帮助。 There are two different times to save state, in onPause and in onSavedInstanceState. 保存状态有两种不同的时间,分别是onPause和onSavedInstanceState。 onPause is the more reliable one, because it is gauranteed to be called as part of the activity lifecycle. onPause是更可靠的一种,因为它被保证被称为活动生命周期的一部分。

If you save in onSaveInstanceState, you should recreate in onCreate. 如果保存在onSaveInstanceState中,则应在onCreate中重新创建。 However, as Sebi pointed out, the way you are killing the application might be preventing onSaveInstanceState from being called at all. 但是,正如Sebi指出的那样,您杀死应用程序的方式可能根本阻止了onSaveInstanceState的调用。

If you want to end your activity, just call finish(). 如果您想结束活动,只需调用finish()。

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

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