Android的静态对象的生命周期 [英] Android static object lifecycle

查看:220
本文介绍了Android的静态对象的生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建事件的搜索应用程序中,我们从一个屏幕设置搜索条件填充在另一个屏幕,然后用户可以编辑从第三屏搜索条件,进入第4个屏幕。

要实现上述任务,我用它记得在应用程序中的值静态对象,我不需要做任何额外的事情。

但我害怕,如果是静态对象的生命周期中的android,如果低内存发现的android删除静态物体???

由于Android支持多任务处​​理,如果用户切换到另一个应用程序,当用户回来的应用程序开始行动疯狂,没有静态对象获取删除时,它的多任务???任何想法 ??同时也建议举行静态对象通过单的方法是更好的方法???

解决方案

让我们开始有一点背景:当你启动应用程序,会发生什么
    操作系统启动一个进程,并为其分配一个唯一的进程ID和分配过程table.A过程开始DVM(Dalvik虚拟机)的一个实例;每个应用程序一个DVM内运行。
一个DVM管理类装载卸载,实例的生命周期,GC等。

静态变量的寿命:静态变量开始存在,当一个类被载入JVM和模具类卸载时

因此​​,如果您创建一个Android应用程序,并初始化静态变量,它会留在JVM中,直到发生以下情况之一:
1.类被卸载
2. JVM关闭
3.进程死掉

请注意,当您切换到另一个应用程序的不同的活动,并没有以上三种情况的静态变量的值将持续存在。如果任何上述三个都发生静态就会失去它的价值。

您可以用code几行测试这个

  1. 打印未初始化的静态在活动的onCreate - >应打印空
  2. 初始化静态。打印 - >值是非零
  3. 点击后退按钮,进入主屏幕。注:主屏幕是另一种活性
  4. 重新启动你的活动 - >静态变量将是非空
  5. 从DDMS(停在设备窗口按钮)杀死你的应用程序。
  6. 重新启动你的活动 - >静态会有null值

希望有所帮助。

I am creating event search application, we set search criteria from one screen populate in another screen then user can edit search criteria from 3rd screen and goes to 4th screen.

To achieve above task i am using static object which remember the values around the application and i don't need to do any thing extra.

But i am afraid if about static object life cycle in android if low memory found android delete static objects ???

As android supports multi tasking, if user switches to another application and when user comes back application start acting crazy, does static object get removed when it multi task ??? any idea ?? and also suggest holding static object via singleton method is better approach ???

解决方案

Lets start with a bit of background: What happens when you start an application?
The OS starts a process and assigns it a unique process id and allocates a process table.A process start an instance of DVM(Dalvik VM); Each application runs inside a DVM.
A DVM manages class loading unloading, instance lifecycle, GC etc.

Lifetime of a static variable: A static variable comes into existence when a class is loaded by the JVM and dies when the class is unloaded.

So if you create an android application and initialize a static variable, it will remain in the JVM until one of the following happens:
1. the class is unloaded
2. the JVM shuts down
3. the process dies

Note that the value of the static variable will persist when you switch to a different activity of another application and none of the above three happens. Should any of the above three happen the static will lose its value.

You can test this with a few lines of code:

  1. print the uninitialized static in onCreate of your activity -> should print null
  2. initialize the static. print it -> value would be non null
  3. Hit the back button and go to home screen. Note: Home screen is another activity.
  4. Launch your activity again -> the static variable will be non-null
  5. Kill your application process from DDMS(stop button in the devices window).
  6. Restart your activity -> the static will have null value.

Hope that helps.

这篇关于Android的静态对象的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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