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

在主活动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。

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


当前回答

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

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

例子:

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");

其他回答

你可以使用intent, Sqlite或Shared Preferences。当涉及到媒体存储时,如文档、照片和视频,您可以创建新文件。

就像上面讨论的那样,操作系统可以在没有任何通知的情况下杀死应用程序(没有onDestroy事件),所以没有办法保存这些全局变量。

SharedPreferences可以是一个解决方案,除非你有复杂的结构化变量(在我的情况下,我有一个整数数组来存储用户已经处理的id)。SharedPreferences的问题在于,每次需要值时都很难存储和检索这些结构。

在我的情况下,我有一个后台服务,所以我可以把这些变量移动到那里,因为服务有onDestroy事件,我可以很容易地保存这些值。

在活动结果之前在简历上被调用。所以移动你的登录检查到恢复和你的第二次登录可以被阻止,一旦第二个活动已经返回积极的结果。On简历每次都会被调用,所以不用担心第一次没有被调用。

您可以使用一个静态字段来存储这种状态。或者把它放到资源Bundle中,然后在onCreate(Bundle savedInstanceState)上从那里恢复。只要确保你完全理解Android应用程序管理的生命周期(例如,为什么login()会在键盘方向改变时被调用)。

你只需要定义一个应用程序的名称,如下所示:

<application
  android:name="ApplicationName" android:icon="@drawable/icon">
</application>