Android应用程序生命周期和singelton [英] Android Application life cycle and singelton

查看:179
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆