2016-07-22 150 views
0

我正在構建一個應用程序,我有一個登錄選項。當存在令牌時,用戶直接進入主活動。這沒關係,但是當我點擊LogOut按鈕時,它會在該頁面上再次向我發送LogOut按鈕,而不是將我發送給LogIn活動。這是爲什麼? 我的MainActivity:我的登錄按鈕不起作用

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

     setContentView(R.layout.activity_main); 
     SharedPreferences shf = getApplicationContext().getSharedPreferences("Token pref", MODE_PRIVATE); 
     String strPref = shf.getString("token", null); 

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

     viewToken = (TextView)findViewById(R.id.tokenView); 
     String data = getIntent().getStringExtra("result"); 


     initializeInjector(); 
     initialize(); 
    } 
@Override 
    protected void onResume() { 
     Log.d("OnResume", "Ovo je onResume"); 


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

     if(strPref == null) { 
      Intent intent = new Intent(this, LoginActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(intent); 
      finish(); 
     } 
     super.onResume(); 

    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     finish(); 

    } 
    buttonOk = (Button)view.findViewById(R.id.buttonOk); 

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

這是我的登錄活動:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.d("onCreateLogIn", "Ovo je onCreateLogIna"); 
    setContentView(R.layout.activity_login); 
    SharedPreferences shf = getSharedPreferences("Token pref", MODE_PRIVATE); 
    String strPref = shf.getString("token", null); 

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

這buttonOk的主要活動是註銷按鈕。

我在做什麼錯?

+0

你的主要活動是什麼觀點? –

+0

這是inflanter,它將我發送到WrongUSerFragment – Atenica

+0

不清楚什麼按鈕是註銷按鈕。你最好更新你的變量,讓他們說註銷 –

回答

0

將下面的代碼到onCreate方法

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

buttonOk = (Button)view.findViewById(R.id.buttonOk); 

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

} 
0

OnClickListener您使用"Token Prefs"喜好裏面:

getSharedPreferences("Token Prefs", Context.MODE_PRIVATE); 
在登錄及主要活動,你

使用Token pref

getSharedPreferences("Token pref", MODE_PRIVATE); 

你沒有操縱相同的預處理ferences其更改爲Token prefOnClickListener

編輯: 我沒有注意到,因爲@SaravInfern說你有你的OnClickListener代碼和buttonOk = (Button)findViewById(....)移動到onCreate方法。