-1

我有一个应用程序,我在其中设置了使用 facebook 登录的选项。用户登录后,我在 Shared Preferences 中存储一个布尔值,以便下次用户打开应用程序时,他会被定向到主屏幕而不是登录屏幕。

我注意到一件事,如果我杀死该应用程序,它会在我下次打开它时要求登录。它是android应用程序的默认行为还是可以修复它以便我的应用程序即使在它被杀死后也能保留登录参数?

我正在使用TinyDB从 SharedPreferences 存储和检索值。

这是我的onCreate方法:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tinyDB = new TinyDB(this);
        final boolean loggedIn = tinyDB.getBoolean("Login", false);
        if(loggedIn) {
            Intent intent = new Intent(LaunchActivity.this, HomeActivity.class);
            startActivity(intent);
            LaunchActivity.this.finish();
        }

        Fabric.with(this, new Crashlytics());
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_launch);
        }

        int SPLASH_TIME_OUT = 3000;
        new Handler().postDelayed(new Runnable() {
            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */

            @Override
            public void run() {
                // This method will be executed once the timer is over
                startLoginOrHome(loggedIn);
            }
        }, SPLASH_TIME_OUT);
    }

    private void startLoginOrHome(boolean loggedIn) {
        if(loggedIn) {
            LaunchActivity.this.finish();
        } else {
            Intent intent = new Intent(LaunchActivity.this, SignInActivity.class);
            startActivity(intent);
            LaunchActivity.this.finish();
        }
    }
4

1 回答 1

0

对我来说,您的问题在于保存布尔值。无需使用 TinyDB,只需在登录时添加以下代码即可。

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(); 
prefs.edit().putBoolean("login", true).commit();

并在 OnCreate 中使用

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(); 
Boolean isLoggedIn = prefs.getBoolean("login", false);

 Log.v(TAG,String.ValueOf(isLoggedIn)); //Use Logging to monitor  

  if(isLoggedIn) {
      goToHomeScreen()
    }
else
    goToLogInScreen()
于 2015-11-20T06:39:57.747 回答