android:如何在onResume时恢复对象,并在onPause时保存它?
我想在暂停时保存一个对象(Myclass),并在应用程序恢复时加载它。
我尝试了一切,但没有任何效果。
我正在尝试创建一个线程并使其在问题出现时在我的主要活动上运行。 当我按下后退按钮(退出应用程序)并再次单击应用程序时,会创建一个新线程并且不会恢复旧线程。
I want to save a object(Myclass) on pause, and load it when the application resumes.
I tried everything but nothing works.
I'm trying to make a thread and make it run on my main activity when the problem comes.
When I press the back button(exit the application) and click again on the application, that creates a new thread and does not resume the old thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使类(MyClass)实现
Serialized
,然后您可以在活动销毁时将其保存为文件,并在恢复时从文件中恢复它Make the class (MyClass) implement
Serializable
, you can then save it as a file when the activity is destroyed, and restore it from file when it's resumed本质上你不能(也不应该)做你想做的事。如果您希望在用户结束会话*后继续执行代码,则您的代码应与服务关联运行。然后,代码应该将其工作写入持久存储,或者您的服务应该允许新创建的活动与线程连接的某些绑定接口。
*如果用户支持我们的活动,会话就会结束。这与用户按 HOME 键的行为不同,后者表明用户希望在返回应用程序时恢复他们正在执行的操作。
Essentially you can't (and shouldn't) do what you're trying to do. If you have code that you want to continue to execute after the user ends a session*, your code should be running in association with a Service. The code should then be writing its work to persistent storage or your Service should allow for some binding interface for the newly created Activity to connect with the Thread.
*A session ends if a user backs our of your Activities. This is a different behavior than the user pressing the HOME key, which indicates that the user wants to resume what they were doing when they return to your application.