-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPreferences.java
48 lines (36 loc) · 1.21 KB
/
getPreferences.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package ch.nicolassauter.testgetpref;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
textView = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("1000");
}
});
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences sh = getPreferences(MODE_PRIVATE);
sh.edit().putString("meinWert",textView.getText().toString()).apply();
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences sh = getPreferences(MODE_PRIVATE);
textView.setText(sh.getString("meinWert","Nichts?"));
}
}