回答編集履歴

3

onResume の追加により onCreate での初期設定は不要になるので, コメント化

2020/03/11 05:20

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -22,11 +22,11 @@
22
22
 
23
23
 
24
24
 
25
- SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(SetupActivity.this);
25
+ //SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(SetupActivity.this);
26
26
 
27
- boolean isChecked = preference.getBoolean(SWITCH1_PREFKEY, false);
27
+ //boolean isChecked = preference.getBoolean(SWITCH1_PREFKEY, false);
28
28
 
29
- switchButton.setChecked(isChecked);
29
+ //switchButton.setChecked(isChecked);
30
30
 
31
31
 
32
32
 

2

getSharedPreferences() を削除

2020/03/11 05:20

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
 
24
24
 
25
- SharedPreferences preference = getSharedPreferences();
25
+ SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(SetupActivity.this);
26
26
 
27
27
  boolean isChecked = preference.getBoolean(SWITCH1_PREFKEY, false);
28
28
 
@@ -60,7 +60,7 @@
60
60
 
61
61
 
62
62
 
63
- SharedPreferences preference = getSharedPreferences();
63
+ SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(SetupActivity.this);
64
64
 
65
65
  SharedPreferences.Editor editor = preference.edit();
66
66
 
@@ -76,12 +76,6 @@
76
76
 
77
77
  }
78
78
 
79
- private SharedPreferences getSharedPreferences() {
80
-
81
- return PreferenceManager.getDefaultSharedPreferences(SetupActivity.this);
82
-
83
- }
84
-
85
79
  ```
86
80
 
87
81
  ```java
@@ -94,7 +88,7 @@
94
88
 
95
89
  final Switch switchButton = findViewById(R.id.switch1);
96
90
 
97
- SharedPreferences preference = getSharedPreferences();
91
+ SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(SetupActivity.this);
98
92
 
99
93
  boolean isChecked = preference.getBoolean(SWITCH1_PREFKEY, false);
100
94
 

1

onResume 追加

2020/03/10 03:05

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -83,3 +83,23 @@
83
83
  }
84
84
 
85
85
  ```
86
+
87
+ ```java
88
+
89
+ @Override
90
+
91
+ protected void onResume() {
92
+
93
+ super.onResume();
94
+
95
+ final Switch switchButton = findViewById(R.id.switch1);
96
+
97
+ SharedPreferences preference = getSharedPreferences();
98
+
99
+ boolean isChecked = preference.getBoolean(SWITCH1_PREFKEY, false);
100
+
101
+ switchButton.setChecked(isChecked);
102
+
103
+ }
104
+
105
+ ```