问题:
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