0

私はLoginActivityを初めて起動する主なアクティビティを持っています。ここeverythigはOKになります。ユーザーがログインすると、ユーザーの情報はsharedpreferencesに保存され(ApiRestにリクエストするため)、次回ログイン時に初めて起動するこの機能は、次回起動時にmainActivityに滞在するため、正常に機能します。この主なアクティビティは3つのフラグメント(ビューページで)が作成されたときに3つのフレームを持ち、apiから自動的に情報を取得するよう要求します。私の問題はmainActivityのAppbarにあります。ユーザーをログアウトするメニュー項目があります。私の意図は、loginActivityにアプリケーションをリダイレクトし、活動のスタックをクリアし、sharedpreferencesをクリアすることです。しかし私の結果はです。 1.私はログインアクティビティにリダイレクトされますが、アプリケーションを終了して再び開くと、メインアクティビティでのメソッドを尊重しません(ログインアクティビティにリダイレクトする必要がある場合)。そしてすぐに開始する3つのフラグメントを作成します要求を行うには、彼らはパラメータがありません(私は共有の設定をクリアするので、nullまたは0です)ので、アプリケーションがクラッシュします。初めてLoginActivityを開く方法が省略されましたか?ここでは、高度なAndroidのログアウト方法と明確なsharedpreferenceが機能していません

でのおかげで、ここでログアウト

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final Variables variableGlobal = (Variables)getApplicationContext(); 
    SharedPreferences sharedPreferences= getSharedPreferences("firstTime",this.MODE_PRIVATE); 
    boolean firstTime = sharedPreferences.getBoolean("firstTime",true); 
    if(firstTime){ 
     SharedPreferences.Editor edit = sharedPreferences.edit(); 
     edit.putBoolean("firstTime", false); 
     edit.commit(); 
     startActivity(new Intent(MainActivity.this, LoginActivity.class)); 
     finish(); 

    }.... 

後に失敗したり省略されている方法で私MainActivityは私のログアウトコードです:ここ

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.logoutmain) { 
//Here i clear the sharedpref datos wich contains the user information 
     SharedPreferences pref = getSharedPreferences("datos",MODE_PRIVATE); 
     SharedPreferences.Editor editorpref = pref.edit(); 
     editorpref.clear(); 
     editorpref.commit(); 

//Here i clear the sharedPref that contains the value (boolean) to say the app if it is the first time or not 
     SharedPreferences pref2 = getSharedPreferences("firstTime",this.MODE_PRIVATE); 
     SharedPreferences.Editor editorpref2 = pref2.edit(); 
     editorpref2.putBoolean("firstTime",true); 
     editorpref2.commit(); 
     System.out.println("shared pref cleared"); 
     Intent intent=new Intent(getApplicationContext(),MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(intent); 
     finish(); 
     return true; 
    }... 

は私loginActivityです:

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

    btnLogin= (Button)findViewById(R.id.btnLogin); 

    api= ApiUtils.getAPIService(); 
    btnLogin.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      login(); 
     } 
    }); 

} 

私は私のアプリを実行すると、それを実行しているfirstTimeであるかどうかをチェックする方法を忘れないでくださいLoginActivityへの起動は正常に動作しています。これは、firsTimeだけが起動され、次回は常にメインアクティビティで開きますが、ログアウトするとLoginにリダイレクトされ、スタックとsharedprefがクリアされますが、初めてMainActivityに入れてアプリをクラッシュさせてしまいます。なぜ??おかげadavanced

+0

まあ、私はあなたが」この「FIRSTTIME」好みをチェックしてはならないと思いますOnCreateメソッドで設定します。あなたはそれをチェックした後にfalseに設定しているので、MainActivityは、ユーザーがアプリケーションを閉じた後にすでにログインしているかのように起動します。 "firstTime"の代わりに "datos"の存在を確認できます。 – Daivid

+0

はい、ログアウト方法で私はsharedpreferencesをクリアしてTRUEにします。ログアウト方法を確認してください。 – matQ

+0

ええ、アクティビティが終了したときにそれをtrueに設定していないのですか? Nabisのコメントはbtwを修正するかもしれない。 – Daivid

答えて

0

に私はあなたのロジックで問題を参照して、クイックフィックスは、あなたのMainActivityに以​​下のコードを使用することです:

boolean notFirstTime = sharedPreferences.getBoolean("firstTime" , false); 
if (! notFirstTime){ 
    SharedPreferences.Editor edit = ... 
+0

if、shuold save edit(firstTime、true)? – matQ

+0

はい、また可読性の向上のためにキーを「not_first_time」に変更してください –

+0

とログアウト方法新しい変更を加えた論理にするには、クリアするか、または編集をfalseとして保存することをお勧めしますか? – matQ

関連する問題