2016-07-25 31 views
2

我正在開發一個應用程序,讓用戶登錄並登錄。當用戶登錄時,他的令牌被記住,當他再次單擊應用程序時,他不必登錄LogIn活動。但問題是,當我登出他時,他會進入LogIn活動,但是當他從LogIn活動退出時,當他再次點擊應用程序時,他直接進入主活動,所以他的記號不會被刪除。如何解決這個問題? 這是我的登錄活動:爲什麼我的令牌未被刪除?

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

    credentials = new Credentials(); 
    login = (Button) findViewById(R.id.btn_login); 

    login.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      username = (TextInputEditText)findViewById(R.id.username); 
      password = (TextInputEditText)findViewById(R.id.password); 

      String getUsername = username.getText().toString(); 
      String getPassword = password.getText().toString(); 

      if (getUsername.length()>0 && getPassword.length()>0) { 
       credentials.setUsername(getUsername); 
       credentials.setPassword(getPassword); 

       allOperations(); 

      } else { 
       Toast.makeText(LoginActivity.this, R.string.empty_fields , Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 

這是我的主要活動:

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

     setContentView(R.layout.activity_main); 
     toolbar = (Toolbar) findViewById(R.id.tool_bar); 
     setSupportActionBar(toolbar); 

     SharedPreferences shf = getSharedPreferences("Token pref", MODE_PRIVATE); 
     final String strPref = shf.getString("token", null); 

     if(strPref == null) { 
      Intent intent = new Intent(MainActivity.this, LoginActivity.class); 
      startActivity(intent); 
      finish(); 
     } 

     initializeInjector(); 
     initialize();} 

這裏是我的註銷按鈕邏輯(它是在片段上的主要活性):

buttonOk.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      SharedPreferences sharedPreferences = getActivity().getSharedPreferences("Token Prefs", Context.MODE_PRIVATE); 
      sharedPreferences.edit().remove("token").apply(); 
      Intent intent = new Intent(context, LoginActivity.class); 
      startActivity(intent); 
      getActivity().finish(); 
     } 
    }); 

和我的清單:

<activity android:name=".view.activities.MainActivity" 
      android:screenOrientation="portrait"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity android:name=".view.activities.LoginActivity" 
     android:screenOrientation="portrait"> 
     <intent-filter> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

有人可以幫我嗎?

+0

迪迪喲檢查你從SharedPreferences得到什麼 – DKV

+0

不,我應該寫什麼? – Atenica

+0

只是創建一個Tost的字符串「strPref」 – DKV

回答

1

您buttonOk的onclick監聽行更改從

sharedPreferences.edit().remove("token").apply(); 

sharedPreferences.edit().remove("token").commit(); 
+0

不,這不起作用... – Atenica

+0

remove'getActivity()。finish();'嘗試它。 –

0

你的SharedPreferences名稱是不同的一個是 「令牌期望」 等是 「令牌的首選項」

+0

我修復了名稱,但問題是一樣的...我真的不知道該怎麼做了...... :( – Atenica

+0

卸載應用程序,然後再試 – DKV

+0

我已卸載,但沒有再次.... – Atenica