我是否正确设置了我的SharedPreferences只访问登录页面一次?

时间:2014-05-20 10:26:40

标签: android facebook login sharedpreferences

每当我在模拟器中运行我的活动时,它总是需要MainActivity(如果没有登录到Facebook然后通过FacebookLoginFragment),然后ProfileActivity ProfileActivity创建了两次(仍然试图找出这个)。我仍在努力理解SharedPreferences课程,所以非常感谢有人向我解释!

理想情景

  • 如果首次启动应用MainActivity,用户将登录Facebook并定向到ProfileActivity
  • MainActivity在用户点击Android
  • 上的按钮时被杀死
  • 如果用户退出应用并稍后恢复,则应在ProfileActivity
  • 启动

问题

  • 我的代码设置是否正确?
  • 如何正确设置,以便只访问一次登录页面?
  • onResume()帮助中实施MainActivity方法吗?

代码在这里:

第一项活动MainActivity

public class MainActivity extends FragmentActivity {

private FacebookLoginFragment mainFragment;

protected LoginButton fbLogin;

private ImageView splashLogo;
protected Animation anim;

private static final String FIRST_LAUNCH = "first_launch";

private SharedPreferences prefs;
private SharedPreferences.Editor editor;


@Override
protected void onCreate(Bundle savedInstanceState) {
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    editor = prefs.edit();
    Intent i;

    //Assume false if the key does not yet exist
    if (prefs.getBoolean(FIRST_LAUNCH, false)) {
        editor.putBoolean(FIRST_LAUNCH, true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null){
            //Add the fragment on initial activity setup
            mainFragment = new FacebookLoginFragment();
            getSupportFragmentManager()
            .beginTransaction()
            .add(android.R.id.content, mainFragment)
            .commit();
        } else {
            //Or set the fragment from restored state info
            mainFragment = (FacebookLoginFragment) getSupportFragmentManager()
                    .findFragmentById(android.R.id.content);
        }

        splashLogo = (ImageView)findViewById(R.id.appLogoFixed);
        anim = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    } else {
        editor.putBoolean(FIRST_LAUNCH, true);
        editor.commit();
        //Go to profile page
        i = new Intent(MainActivity.this, ProfileActivity.class);
        startActivity(i);
        finish();
    }
}

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus)
        splashLogo.startAnimation(anim);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

第二项活动:ProfileActivity

public class ProfileActivity extends Activity {
protected ProfilePictureView profilePicture;

private TextView userInfoTextView;
private String userId;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile_facebook);

    Intent i = getIntent(); //Grab the intent from previous activity
    userId = i.getStringExtra("Fb_id");

    profilePicture = (ProfilePictureView)findViewById(R.id.profilePicture);
    profilePicture.setProfileId(userId);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onResume() {
    super.onResume();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onPause(){
    super.onPause();
}

@Override
public void onDestroy(){
    super.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
}
}

1 个答案:

答案 0 :(得分:1)

你的FIRST_LAUNCH变量对我来说有点混乱,因为我预计它在第一次启动时会true,因为它尚未设置。然后将其设置为false,因为它不再是“首次启动”了。

无论如何,我在MainActivity的代码的第一个块中看到了> onCreate,你设置

editor.putBoolean(FIRST_LAUNCH, true);

但您可能忘记执行editor.commit(),因此您的新偏好设置永远不会保存。

编辑:只是很简短的解释。似乎您正确理解SharedPreferences。记住这一点非常重要,直到你致电editor.commit()所有的变化都没有保存!如果您不致电commit,您的更改将会丢失。