Scala 对象能否在 Android 上的活动重启后继续存在?
我正在用 Scala 编写一个 Android 应用程序,但我一直无法找到这个问题的明确答案。
我的应用程序包含一个对象,其中包含在 vals 中定义的一堆静态数据。数据包括类的实例。
我的问题是,当 Android 决定终止 Activity 并稍后重新启动它时,我的对象会发生什么情况?据我所知,Scala 中的对象可用于实现与 Java 中的静态值类似的目的,但实际上在生成的字节码中并不是以这种方式实现的。那么 Android 是否知道在重新启动 Activity 时重新初始化我的对象?是否存在不这样做的情况,或者我必须小心的情况?
如果上面的答案是“一切都很好”,我认为由可变数据组成的对象将完全不同。在这种情况下,我非常确定我需要显式保存/恢复此类对象以保留状态。但必须保存/恢复始终相同且硬连接到 APK 本身的数据似乎很愚蠢。
I'm writing an Android app in Scala, and I haven't been able to find a clear answer to this question.
My application contains an object with a bunch of static data defined in vals. The data includes instances of classes.
My question is, what happens to my object when Android decides to kill the activity and later restarts it? I understand that objects in Scala can be used to achieve a similar purpose to static values in Java, but are not actually implemented that way in the generated bytecode. So does Android know to re-initialize my object when it restarts the activity? Are there circumstances where it would not do so, or where I have to be careful?
If the answer to the above is "all is fine", I gather that an object composed of mutable data would be quite different. In that case I'm pretty sure that I would need to explicitly save/restore such objects to retain the state. But it seems silly to have to save/restore data that is always the same and is hard-wired into the APK itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之,对象被转换为单例,并且单个实例保存在静态最终字段中。因此,对象将被重新初始化,并且您需要序列化来恢复可变对象的相同数据。
《Scala 编程》(来自 Odersky 等人),第 31 章,解释了如何使用反编译器来获得您需要的答案 - 这非常简单。
基本上,给定一个这样的对象:
它将被转换为一个名为
App$
的类,实现单例模式。特别是,单例实例包含在定义为public static Final App$ MODULE$ = new App$();
的字段中,并且方法被定义为实例方法。javap App$
可用于确认字段的声明 - jad 可能还可以反编译源代码。如果没有定义伴随类,Scala 还将定义一个名为 App 的类,其中包含要从 Java 调用的相应静态最终方法。这样,您就可以获得具有正确签名的App.main
方法,并且可以调用scala App
,后者又会使用正确的签名调用java App
额外的库。In short, objects are translated to singletons, and the single instance is held in a static final field. Therefore objects will be reinitialized, and you'll need serialization to restore the same data for mutable objects.
Programming in Scala (from Odersky et al.), chapter 31, explains how to use the decompiler to get the answer you need - it's very easy.
Basically, given an object like this:
It will be translated to a class named
App$
implementing the singleton pattern. In particular, the singleton instance is contained within a field defined aspublic static final App$ MODULE$ = new App$();
, and the methods are defined as instance methods.javap App$
can be used to confirm the declaration of the field - probably jad can decompile also the source code. If no companion class is defined, Scala will also define a class namedApp
containing corresponding static final methods to call from Java. This way, you get anApp.main
method with the right signature and can invokescala App
, which in turn invokesjava App
with the right additional libraries.