Android中应用享元全局变量
程序员文章站
2022-05-12 09:05:24
...
在Intent中来回的传一些所谓的全局变量实在苦手,上网搜了搜,果然有省事的方案(http://*.com/questions/708012/android-how-to-declare-global-variables)可以用继承Application来享元~~直接贴代码~
public class ShareContext extends Application { private PHPRPC_Client client; public PHPRPC_Client getClient() { return client; } public void setClient(PHPRPC_Client client) { this.client = client; } }
然后在AndroidManifest.xml中设置name属性
<application android:name=".ShareContext" android:icon="@drawable/icon" android:label="@string/app_name">
设置享元:
ShareContext shareContext = ((ShareContext)getApplicationContext()); shareContext.setClient(client);
读取享元:
ShareContext shareContext = ((ShareContext)getApplicationContext()); PHPRPC_Client client = shareContext.getClient();
好东西~~直接省了很多事!!