Android中静态变量的生命周期

Android中静态变量的生命周期

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

问题描述

当我在主活动中将一个变量声明并初始化为静态变量并且活动被破坏时.我还能访问变量的内容吗?

when I declare and initialize a variable as static in my main activity and the activity gets destroyed. Can I still access the content of the variable?

例如,要始终访问我存储到此变量的 AsyncTask?我想要的是在方向改变后也能访问它.

For example to always access a AsyncTask which I store to this variable?What I want is to be able to access to it also after an orientation change.

推荐答案

静态变量与类相关联,只要类在内存中,它们就会一直存在,并在类卸载时销毁(这种情况很少发生).

Static variables are associated with a class and they will live as long as the class is in the memory,and destroy when class gets unloaded (which very rarely happens).

在 Android 中,您已经看到当我们关闭任何应用程序时,它不会完全关闭,它保留在最近的应用程序堆栈中,您可以通过按住主页按钮(在大多数设备上)看到这一点.

In Android you have seen that when we close any application it does not close completely, It remains in the recent application stack, That you can see by holding in the home button(On Most Devices).

当其他应用需要时,Android 本身会踢掉那些最近的应用记忆

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

09-02 11:25