2014-01-12 82 views
1

嗨,我試圖加載片段時,返回到應用程序。我試圖通過在我的onResume()中的onPause()和session =「active」中設置系統偏好設置中的字符串來實現這一點,該字符串中的session =「not active」;問題是當我導航到一個新的活動,它也運行onPause(),因此設置會話不活動。結果是每次我從一個活動到另一個活動時,我的片段都會加載。我應該提及片段包含一個引腳輸入以保證數據的安全,這就是爲什麼我需要它。所以總結起來有沒有人知道如何檢測用戶是否返回到應用程序?當用戶重新打開應用程序時加載片段android

繼承人是我迄今試圖

@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(); 

    }  

} 

userPin(),然後在我的onCreate運行();

回答

1

你只是不能使用onPause和onResume這個。有關如何執行此操作的解決方案,請參閱this post

相關問題