Programming/Android Function
SharedPreferences (임시저장)
2swan
2023. 8. 5. 15:17
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity4"
android:orientation="vertical">
<EditText
android:id="@+id/et_save"
android:layout_width="100dp"
android:layout_height="wrap_content"/>
</LinearLayout>
main.activity
public class MainActivity4 extends AppCompatActivity {
EditText et_save;
String shared = "file";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
et_save = (EditText) findViewById(R.id.et_save);
SharedPreferences sharedPreferences = getSharedPreferences(shared, 0);
String value = sharedPreferences.getString("lee","");
et_save.setText(value);
}
@Override
protected void onDestroy() {
super.onDestroy();
SharedPreferences sharedPreferences = getSharedPreferences(shared,0);
SharedPreferences.Editor editor = sharedPreferences.edit();
String value = et_save.getText().toString();
editor.putString("lee",value);
editor.commit();
}
}
결과 값
- 입력된 값이 앱을 나간 후에 다시 접속 하였을 때 저장됨
- 앱을 삭제하면 적용되지는 않음