我正在创建一个需要登录的应用程序。我创建了主活动和登录活动。

在主活动onCreate方法中,我添加了以下条件:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ...

    loadSettings();
    if(strSessionString == null)
    {
        login();
    }
    ...
}

当登录表单终止时执行的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);
            }
        }
    }

问题是登录表单有时出现两次(login()方法被调用两次),也当电话键盘滑动登录表单再次出现,我猜问题是变量strSessionString。

有人知道如何设置变量全局,以避免登录表单出现后,用户已经成功验证?


当前回答

Soonil建议的保持应用程序状态的方法是好的,但是它有一个弱点——在某些情况下,操作系统会终止整个应用程序进程。这里是关于此的文档—流程和生命周期。

考虑一个案例-你的应用程序进入后台,因为有人正在呼叫你(电话应用程序现在在前台)。在这种情况下&&在其他一些情况下(检查上面的链接了解它们可能是什么),操作系统可能会杀死你的应用程序进程,包括application子类实例。结果,状态丢失了。当您稍后返回到应用程序时,操作系统将恢复其活动堆栈和application子类实例,但myState字段将为空。

AFAIK,保证状态安全的唯一方法是使用任何类型的持久化状态,例如为应用程序文件使用私有或sharedpreferences(它最终在内部文件系统中为应用程序文件使用私有)。

其他回答

如何使用这样的全局结构来确保本机内存的收集呢?

活动有一个onPause/onDestroy()方法,在销毁时调用,但Application类没有等价物。当应用程序被杀死或任务栈被放到后台时,建议采用什么机制来确保全局结构(特别是那些包含对本机内存的引用)被适当地垃圾收集?

不要在manifest文件中使用另一个<application>标签。只需要在现有的<application>标签中做一个改变,添加这一行android:name="。ApplicationName”,其中,ApplicationName将是您即将创建的子类的名称(用于存储全局)。

所以,最后你的manifest文件中的ONE AND ONLY <application>标签应该是这样的:-

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.NoActionBar"
        android:name=".ApplicationName"
        >

Soonil建议的保持应用程序状态的方法是好的,但是它有一个弱点——在某些情况下,操作系统会终止整个应用程序进程。这里是关于此的文档—流程和生命周期。

考虑一个案例-你的应用程序进入后台,因为有人正在呼叫你(电话应用程序现在在前台)。在这种情况下&&在其他一些情况下(检查上面的链接了解它们可能是什么),操作系统可能会杀死你的应用程序进程,包括application子类实例。结果,状态丢失了。当您稍后返回到应用程序时,操作系统将恢复其活动堆栈和application子类实例,但myState字段将为空。

AFAIK,保证状态安全的唯一方法是使用任何类型的持久化状态,例如为应用程序文件使用私有或sharedpreferences(它最终在内部文件系统中为应用程序文件使用私有)。

class GlobaleVariableDemo extends Application {

    private String myGlobalState;

    public String getGlobalState(){
     return myGlobalState;
    }
    public void setGlobalState(String s){
     myGlobalState = s;
    }
}

class Demo extends Activity {

@Override
public void onCreate(Bundle b){
    ...
    GlobaleVariableDemo appState = ((GlobaleVariableDemo)getApplicationContext());
    String state = appState.getGlobalState();
    ...
    }
}

你可以使用两种方法来做到这一点:

使用应用程序类 使用共享首选项 使用应用程序类

例子:

class SessionManager extends Application{

  String sessionKey;

  setSessionKey(String key){
    this.sessionKey=key;
  }

  String getSessisonKey(){
    return this.sessionKey;
  }
}

你可以使用上面的类在MainActivity中实现登录。代码看起来像这样:

@override 
public void onCreate (Bundle savedInstanceState){
  // you will this key when first time login is successful.
  SessionManager session= (SessionManager)getApplicationContext();
  String key=getSessisonKey.getKey();
  //Use this key to identify whether session is alive or not.
}

This method will work for temporary storage. You really do not any idea when operating system is gonna kill the application, because of low memory. When your application is in background and user is navigating through other application which demands more memory to run, then your application will be killed since operating system given more priority to foreground processes than background. Hence your application object will be null before user logs out. Hence for this I recommend to use second method Specified above.

使用共享首选项。 字符串MYPREF = " com.your.application.session " SharedPreferences pref= context.getSharedPreferences(MyPREF,MODE_PRIVATE); //插入键如下: 编辑器编辑= pre .edit(); editor.putString(“关键”、“价值”); editor.commit (); //获取密钥如下所示。 sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); String key= getResources().getString("key");