문제

I wrote an application with login/logout logic. After login(set user and pass as vars) the user can minimize the application.

Minimize code:

Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);

When user clicks on the app icon in first activity I check is have set user and pass as vars, if they are app go to activity 2(next activity). if they are not set go to login interface.

Everything is working fine but sometimes the app forgets the user and pass after minimizing and I go to the login interface....

Is like clearing cache I don't know... help

도움이 되었습니까?

해결책 2

you should use sharedPreferance to store login user and password. below is code

private void storeUserPreferences(String key, String value) {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        Editor editor = sharedPreferences.edit();
        editor.putString("username", "xyz");
                editor.putString("pass", "123");
        editor.commit();
    }


private void loadUserPreferences() {
            SharedPreferences sharedPreferences = PreferenceManager
                    .getDefaultSharedPreferences(this);
             String pass =sharedPreferences.getString("pass","");
             String username=sharedPreferences.getString("username","");
             if(!TextUtils.isEmpty(pass)&&!TextUtils.isEmpty(username))
             {
                login.....success
             }
             else
             {
                  redirect to login screen
               }



        }

this is ref doc http://developer.android.com/reference/android/content/SharedPreferences.html

다른 팁

In Android, you cannot assume that your application will be kept in memory when it goes to the background, and you cannot assume it stays on the foreground (people may press the Home key or popups may come up). You should implement the onPause and onResume events and store the details of the logged in user there. These methods are guaranteed to be called by Android whenever your application goes into the background and is re-activated, respectively. You can use the SavedBundle object that you get in these methods to store your data. Also read about the app lifecycle here: http://developer.android.com/reference/android/app/Activity.html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top