嗨我正在尝试在返回应用时加载片段。我试图通过在我的onResume()中的onPause()和session =“active”中的系统首选项中设置一个字符串来表示session =“not active”;问题是当我导航到一个新的活动时,它也会运行onPause(),从而将会话设置为不活动。结果是,每当我从一个活动转到另一个活动时,我的片段就会加载。我应该提一下,该片段包含一个引脚输入,以保证数据安全,这就是我需要它的原因。总结一下,有没有人知道如何检测用户何时返回应用程序?
继承人到目前为止我所尝试过的事情
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
EasyTracker.getInstance(this).activityStop(this); // Add this method.
saveStringToPreference(this, "user_session", "not_active");
Log.v("Homepage", "onStop");
Log.v("Homepage", "user_session= " + returnStringFromPreference(this, "user_session"));
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
saveStringToPreference(this, "user_session", "not_active");
Log.v("Homepage", "onDestroy");
Log.v("Homepage", "user_session= " + returnStringFromPreference(this, "user_session"));
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
saveStringToPreference(this, "user_session", "active");
ActionBar actionBar = getActionBar();
actionBar.hide();
Fragment pinFragment = new PinFragment();
FragmentManager pinFragmentManager = getFragmentManager();
pinFragmentManager.beginTransaction().replace(R.id.content_frame, pinFragment).commit();
Log.v("Homepage", "onResume");
Log.v("Homepage", "user_session= " + returnStringFromPreference(this, "user_session"));
}
public void userPin(){
if(userHasSetPin(this) == true){
if(returnStringFromPreference(this, "user_session").equals("active")){
fragment = new homePageFragment();
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
}else{
ActionBar actionBar = getActionBar();
actionBar.hide();
Fragment pinFragment = new PinFragment();
FragmentManager pinFragmentManager = getFragmentManager();
pinFragmentManager.beginTransaction().replace(R.id.content_frame, pinFragment).commit();
}
}else{
ActionBar actionBar = getActionBar();
actionBar.hide();
Fragment pinSetupFragment = new PinSetupFragment();
FragmentManager pinSetupFragmentManager = getFragmentManager();
pinSetupFragmentManager.beginTransaction().replace(R.id.content_frame, pinSetupFragment).commit();
}
}
然后在我的onCreate();
中运行userPin()