Android Application life cycle and singelton

心不动则不痛 提交于 2020-01-02 05:36:07

问题


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;
    }
 }

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";
    }
}

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.

why does it happening?

what am I missing?

when android garbage collecting static members belongs to application?


回答1:


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.

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.




回答2:


You can not control when exactly Java objects become garbage collected. An object becomes eligible for garbage collection when there are no more (non-circular) references to it. With Android, further, you can not control when your Activity gets removed from memory.




回答3:


why does it happening?

what am I missing?

when android garbage collecting static members belongs to application?

Ok first, as others said, there is no close application concept on Android as the android OS manages lifecycle of your application process on their own.

Second, you did the wrong test - if instead of closing all apps you would do the opposite - that is - fill up memory by starting more and more apps, then eventually your application's memory would be cleaned up to be used by other applications and this includes all static mebers as well as instance members! then, you will see that the static variable WILL BE NULL as you expected.

They just "lazily" clean up memory, if there's enough memory then you application might never get cleaned up.

Actually, there is no way around it, as far as i know, there is no way to grauntee an object would not be cleaned up at any point from the device memory. in somecases it leads to bad behaviour. such as if the singleton does heavy processing on its creation, calling getInstance might get your UI stuck, or even make your app crash due to irresponsibleness.



来源:https://stackoverflow.com/questions/10761488/android-application-life-cycle-and-singelton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!