如何在Android中聲明全局變量? - How to declare global variables in Android?

問題:

I am creating an application which requires login. 我正在建立一個須要登陸的應用程序。 I created the main and the login activity. 我建立了主要和登陸活動。 app

In the main activity onCreate method I added the following condition: 在主要活動onCreate方法中我添加了如下條件: ide

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方法以下所示: ui

@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 this

Does anyone know how to set the variable global in order to avoid login form appearing after the user already successfully authenticates? 有沒有人知道如何設置變量global以免在用戶已經成功驗證後出現登陸表單? spa


解決方案:

參考一: https://stackoom.com/question/2yBY/如何在Android中聲明全局變量
參考二: https://oldbug.net/q/2yBY/How-to-declare-global-variables-in-Android
相關文章
相關標籤/搜索