Android应用程序生命周期和singelton

编程入门 行业动态 更新时间:2024-10-28 14:23:04
本文介绍了Android应用程序生命周期和singelton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以及我们最熟悉的模式:

well most of us familiar with this pattern:

public class MySingeltone { public String mSomeReferenceTypeData; public int mSomeValueTypeData; private static MySingeltone mInstance; private MySingeltone() { } public static MySingeltone getInstance() { if (mInstance == null) { mInstance = new MySingeltone(); } return mInstance; } }

我的问题是,我最近发现,mInstance用做他被破坏,或活动后,不等于空当整个应用程序假设是条款,例如:

my problem is that I've found recently that the mInstance don't equal null after activity using him been destroyed, or when the whole application suppose to be clause, for example:

public class SomeActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MySingeltone mySingeltone = MySingeltone.getInstance(); mySingeltone.mSomeReferenceTypeData = "some value"; } }

关闭运行的活动,整个应用程序启动后,当SomeActivity下一次

(说10秒后..)的mInstance仍持有相同的参考,在他的领域相同的值。

when launching "SomeActivity" next time after closing the whole applications running activities (say 10 seconds after..) the mInstance still holds the same reference, with the same values on his fields.

为什么会发生?

我缺少什么?

在Android的垃圾回收静态成员属于应用程序?

when android garbage collecting static members belongs to application?

推荐答案

由于mInstance是一个静态变量,当你关闭你的应用程序就不会得到空。应用程序的关闭并不意味着你的应用得到了销毁。

Since "mInstance" is a static variable it will not get null when you close your application. Closing of application doesn't means that your application got destroyed.

也没有关闭您的Andr​​oid应用程序的概念。如果你离开你的应用程序中它不会在同一时间销毁。 Android操作系统处理它的内部时,关闭应用程序时,它不再使用。在内存不足的情况​​下,当Android的决定破坏应用程序,然后这个静态变量也得到了空。

Also there is no concept of Closing your Android app. If you get out of your app it will not get destroyed at the same time. Android OS handles it internally when to close the app when it is no more in use. In case of memory shortage when android decides to destroy the app then this static variable will also got null.

更多推荐

Android应用程序生命周期和singelton

本文发布于:2023-11-17 04:17:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608798.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:生命周期   应用程序   Android   singelton

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!