每当我在模拟器中运行我的活动时,它总是需要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);
}
}
答案 0 :(得分:1)
你的FIRST_LAUNCH
变量对我来说有点混乱,因为我预计它在第一次启动时会true
,因为它尚未设置。然后将其设置为false
,因为它不再是“首次启动”了。
无论如何,我在MainActivity的代码的第一个块中看到了> onCreate,你设置
editor.putBoolean(FIRST_LAUNCH, true);
但您可能忘记执行editor.commit()
,因此您的新偏好设置永远不会保存。
编辑:只是很简短的解释。似乎您正确理解SharedPreferences
。记住这一点非常重要,直到你致电editor.commit()
所有的变化都没有保存!如果您不致电commit
,您的更改将会丢失。