티스토리 뷰

Android/Java

[Java]  SharedPreferences  +  Singleton Pattern

혀가 길지 않은 개발자 2020. 6. 30. 22:54

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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=".MainActivity">

    <TextView
        android:id="@+id/tvId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Id : "
        android:textSize="30dp"
        android:layout_marginLeft="30dp"
        android:layout_marginBottom="240dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

    <EditText
        android:id="@+id/etId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="30dp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:ems="6"
        android:inputType="number"
        app:layout_constraintTop_toTopOf="@+id/tvId"
        app:layout_constraintLeft_toRightOf="@+id/tvName"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/tvId" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name : "
        android:textSize="30dp"
        android:layout_marginTop="30dp"
        app:layout_constraintTop_toBottomOf="@+id/tvId"
        app:layout_constraintLeft_toLeftOf="@+id/tvId"/>

    <EditText
        android:id="@+id/etName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="30dp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:ems="6"
        android:inputType="text"
        app:layout_constraintTop_toTopOf="@+id/tvName"
        app:layout_constraintLeft_toRightOf="@+id/tvName"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/tvName"/>

    <TextView
        android:id="@+id/tvAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Age : "
        android:textSize="30dp"
        android:layout_marginTop="30dp"
        app:layout_constraintTop_toBottomOf="@+id/tvName"
        app:layout_constraintLeft_toLeftOf="@+id/tvId"/>

    <EditText
        android:id="@+id/etAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:ems="6"
        android:inputType="number"
        app:layout_constraintTop_toTopOf="@+id/tvAge"
        app:layout_constraintLeft_toRightOf="@+id/tvName"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/tvAge"/>

    <Button
        android:id="@+id/btnSave"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:textAllCaps="false"
        android:text="Save"
        android:textSize="18dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="60dp"
        app:layout_constraintTop_toBottomOf="@+id/etAge"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <Button
        android:id="@+id/btnLoad"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:textAllCaps="false"
        android:text="Load"
        android:textSize="18dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        app:layout_constraintTop_toBottomOf="@+id/btnSave"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml

 

 

 

 

 

 

MainActivity.java

package com.jwsoft.javaproject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText etId;
    EditText etName;
    EditText etAge;
    Button btnSave;
    Button btnLoad;

    SharedPreferences sp;
    SharedPreferences.Editor editor;

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

        initializeView();

        sp = getSharedPreferences("SharedPreferencesFile", MODE_PRIVATE);
        editor = sp.edit();

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = etId.getText().toString();
                String name = etName.getText().toString();
                String age = etAge.getText().toString();

                if (!id.equals("")) {
                    editor.putLong("id", Long.parseLong(id));
                }
                if (!name.equals("")) {
                    editor.putString("name", name);
                }
                if (!age.equals("")) {
                    editor.putInt("age", Integer.parseInt(age));
                }

                editor.apply();     // thread non block (asynchronous)
//                editor.commit();    // thread block (synchronous)
            }
        });

        btnLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Long id = sp.getLong("id", 0);
                String name = sp.getString("name", "");
                int age = sp.getInt("age", 0);

                etId.setText(Long.toString(id));
                etName.setText(name);
                etAge.setText(Integer.toString(age));
            }
        });

    }

    void initializeView() {
        etId = findViewById(R.id.etId);
        etName = findViewById(R.id.etName);
        etAge = findViewById(R.id.etAge);
        btnSave = findViewById(R.id.btnSave);
        btnLoad = findViewById(R.id.btnLoad);
    }

}

실행 결과

 

 

 

 

 

 

 

 


SharedPreferences  +  Singleton Pattern


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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=".MainActivity">

    <TextView
        android:id="@+id/tvId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Id : "
        android:textSize="30dp"
        android:layout_marginLeft="30dp"
        android:layout_marginBottom="240dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

    <EditText
        android:id="@+id/etId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="30dp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:ems="6"
        android:inputType="number"
        app:layout_constraintTop_toTopOf="@+id/tvId"
        app:layout_constraintLeft_toRightOf="@+id/tvName"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/tvId" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name : "
        android:textSize="30dp"
        android:layout_marginTop="30dp"
        app:layout_constraintTop_toBottomOf="@+id/tvId"
        app:layout_constraintLeft_toLeftOf="@+id/tvId"/>

    <EditText
        android:id="@+id/etName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="30dp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:ems="6"
        android:inputType="text"
        app:layout_constraintTop_toTopOf="@+id/tvName"
        app:layout_constraintLeft_toRightOf="@+id/tvName"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/tvName"/>

    <TextView
        android:id="@+id/tvAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Age : "
        android:textSize="30dp"
        android:layout_marginTop="30dp"
        app:layout_constraintTop_toBottomOf="@+id/tvName"
        app:layout_constraintLeft_toLeftOf="@+id/tvId"/>

    <EditText
        android:id="@+id/etAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:ems="6"
        android:inputType="number"
        app:layout_constraintTop_toTopOf="@+id/tvAge"
        app:layout_constraintLeft_toRightOf="@+id/tvName"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/tvAge"/>

    <Button
        android:id="@+id/btnSave"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:textAllCaps="false"
        android:text="Save"
        android:textSize="18dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="60dp"
        app:layout_constraintTop_toBottomOf="@+id/etAge"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <Button
        android:id="@+id/btnLoad"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:textAllCaps="false"
        android:text="Load"
        android:textSize="18dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        app:layout_constraintTop_toBottomOf="@+id/btnSave"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml

 

 

 

 

 

 

 

SharedPreferencesManager.java

package com.jwsoft.javaproject;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesManager {

    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;

    public static synchronized SharedPreferences getInstance(Context context) {
        sp = context.getSharedPreferences("SharedPreferencesFile", Context.MODE_PRIVATE);
        return sp;
    }

    public static void putLong(Context context, String key, long value) {
        editor = getInstance(context).edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public static long getLong(Context context, String key, long defaults) {
        return getInstance(context).getLong(key, defaults);
    }

    public static void putString(Context context, String key, String value) {
        editor = getInstance(context).edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static String getString(Context context, String key, String defaults) {
        return getInstance(context).getString(key, defaults);
    }

    public static void putInt(Context context, String key, int value) {
        editor = getInstance(context).edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public static int getInt(Context context, String key, int defaults) {
        return getInstance(context).getInt(key, defaults);
    }

}

 

 

 

 

 

 

 

 

MainActivity.java

package com.jwsoft.javaproject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText etId;
    EditText etName;
    EditText etAge;
    Button btnSave;
    Button btnLoad;

    Context context;

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

        initializeView();

        context = getApplicationContext();

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = etId.getText().toString();
                String name = etName.getText().toString();
                String age = etAge.getText().toString();

                if (!id.equals("")) {
                    SharedPreferencesManager.putLong(context,"id", Long.parseLong(id));
                }
                if (!name.equals("")) {
                    SharedPreferencesManager.putString(context, "name", name);
                }
                if (!age.equals("")) {
                    SharedPreferencesManager.putInt(context, "age", Integer.parseInt(age));
                }
            }
        });

        btnLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Long id = SharedPreferencesManager.getLong(context, "id", 0);
                String name = SharedPreferencesManager.getString(context, "name", "");
                int age = SharedPreferencesManager.getInt(context, "age", 0);

                etId.setText(id.toString());
                etName.setText(name);
                etAge.setText(Integer.toString(age));
            }
        });

    }

    void initializeView() {
        etId = findViewById(R.id.etId);
        etName = findViewById(R.id.etName);
        etAge = findViewById(R.id.etAge);
        btnSave = findViewById(R.id.btnSave);
        btnLoad = findViewById(R.id.btnLoad);
    }

}

실행 결과

 

 

 

 

 

 

 

'Android > Java' 카테고리의 다른 글

[Java]  BottomSheetDialogFragment  (0) 2020.07.01
[Java]  BottomSheetDialog  (0) 2020.07.01
[Java]  Handler   vs   runOnUiThread   vs   AsyncTask  (0) 2020.06.29
[Java]  AsyncTask  (0) 2020.06.29
[Java]  runOnUiThread  (0) 2020.06.28
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함