2016-04-11 8 views
-3

私は同様の疑問点を確認して、動作するように見えます。私は問題と思われるものを理解することはできません。値は、すべてのアプリケーションの再起動またはアクティビティの切り替え後に0になります。アクティビティを切り替えるかアプリを閉じると、Androidの共有設定が保存されません

//just parts of code from activity1 
      SharedPreferences pref; 
      SharedPreferences.Editor editor; 
// On create.... 
      pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
      editor = pref.edit(); 
      max=pref.getInt("mxtime", 0); 

//If something>something... 
      editor.putInt("mxtime", max); 
      editor.commit(); 

最初の部分では、私はメインアクティビティでSharedPreferencesを宣言します。私は "max" intに保存し、空の値が0の場合は起動時に常に0にします。2番目のアクティビティでは、ボタンをクリックするとSharedPreferencesの値を空にする必要があります。

活動2:

public class settings extends AppCompatActivity { 
private Button myButton; 
private Button myButton2; 
private Button myButton3; 
//sharedPrefs 
SharedPreferences pref; 
SharedPreferences.Editor editor; 

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

    pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
    editor = pref.edit(); 
    myButton = (Button) findViewById(R.id.button3); 
    myButton2 = (Button) findViewById(R.id.button4); 
    myButton3 = (Button) findViewById(R.id.button5); 

    myButton.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v) { 
      Intent i = new Intent(getApplicationContext(),MainActivity.class); 
      startActivity(i); 


     } 
    }); 
    myButton2.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v) { 
      //sharedPrefs 
      editor.remove("mxtime"); 
      editor.commit(); 


     } 
    }); 
} 

}

+0

**なぜ**あなたの好み 'editor.remove( "mxtime")から好みを削除するには、'? **ここで**は 'max'の値を設定しますか? –

+0

私はSharedPrefsに保存する直前にmaxの値を設定しました。そしてeditor.clearをeditor.clear()に変更しました。 – ScaredFace

+0

**なぜあなたの設定 'editor.clear();'からすべての設定を削除しますか?そして** **はあなたが 'max'に割り当てる値ですか? –

答えて

0

このようなSharedPreferencesを使用してみてください:

SharedPreferences preferences = getApplicationContext().getSharedPreferences("loginPref", MODE_PRIVATE); 
SharedPreferences.Editor editor = preferences.edit(); 
+0

これは私がそれを試してみたときに働いたのですが、2回目に試してみましたが、再び0になりました。何が間違っているのか分かりません – ScaredFace

+0

commit()の代わりにeditor.apply()を使用しようとしました –

+0

試しても何も動作していないようです... – ScaredFace

0

アクティビティ1:

SharedPreferences sp = getSharedPreferences("YourSharedPreference", Activity.MODE_PRIVATE); 
SharedPreferences.Editor editor = sp.edit(); 
int DEFAULT_VALUE = 0; 
editor.putInt("VARIABLE_KEY", DEFAULT_VALUE); 

//If something > something.. 

int VALUE_TO_PASS = <your value here>; 
editor.putInt("VARIABLE_KEY", VALUE_TO_PASS); 

// Before screen shift 

editor.commit(); 

....... ....................................

活動2:

SharedPreferences sp = getSharedPreferences("YourSharedPreference", Activity.MODE_PRIVATE); 
int DEFAULT_FALLBACK_VALUE = 0; //When value is not received, show this 

int VALUE_PASSED = sp.getInt("VARIABLE_KEY", DEFAULT_FALLBACK_VALUE); 

// On button click: 

int DEFAULT_VALUE = 0; 
editor.putInt("VARIABLE_KEY", DEFAULT_VALUE); 
editor.commit(); 
関連する問題