android方法中定义变量,如何在Android中声明全局变量? - How to declare global variables in Android?...

问题:

I am creating an application which requires login. 我正在创建一个需要登录的应用程序。 I created the main and the login activity. 我创建了主要和登录活动。

In the main activity onCreate method I added the following condition: 在主要活动onCreate方法中我添加了以下条件:

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

...

loadSettings();

if(strSessionString == null)

{

login();

}

...

}

The onActivityResult method which is executed when the login form terminates looks like this: 登录表单终止时执行的onActivityResult方法如下所示:

@Override

public void onActivityResult(int requestCode,

int resultCode,

Intent data)

{

super.onActivityResult(requestCode, resultCode, data);

switch(requestCode)

{

case(SHOW_SUBACTICITY_LOGIN):

{

if(resultCode == Activity.RESULT_OK)

{

strSessionString = data.getStringExtra(Login.SESSIONSTRING);

connectionAvailable = true;

strUsername = data.getStringExtra(Login.USERNAME);

}

}

}

The problem is the login form sometimes appears twice (the login() method is called twice) and also when the phone keyboard slides the login form appears again and I guess the problem is the variable strSessionString . 问题是登录表单有时会出现两次( login()方法被调用两次),当手机键盘滑动时,再次出现登录表单,我猜问题是变量strSessionString 。

Does anyone know how to set the variable global in order to avoid login form appearing after the user already successfully authenticates? 有没有人知道如何设置变量global以避免在用户已经成功验证后出现登录表单?

解决方案:参考一:

https://stackoom.com/question/2yBY/如何在Android中声明全局变量

参考二:

https://oldbug.net/q/2yBY/How-to-declare-global-variables-in-Android

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值