ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [13] SharedPreferences
    Develpment/Android Using Sample 2020. 9. 13. 21:37

    1. SharedPreferences


     Activity 

     

    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
    public class MainActivity extends AppCompatActivity {
     
        SharedPreferences pref;
        int num;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            pref = PreferenceManager.getDefaultSharedPreferences( MainActivity.this );
     
            num = pref.getInt("save"0);
     
        }// onCreate()
     
        @Override
        protected void onDestroy() {
            super.onDestroy();
     
            SharedPreferences.Editor edit = pref.edit();
     
            edit.putInt("save", num);
            edit.commit();
        }
    }
    cs

    * getDefaultSharedPreferences() : 이전에 저장된 값 불러오기.

    * getIntint("save", 0) : key 값이 save 인 정보를 불러온다. ( default : 0 )

    * onDestroy(), onPause() 등 종료시에 호출 되는 메서드에서 값을 저장.



    2. Tutorial ( SharedPreferences )


     Layout (Main)

     

    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
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.sin.sample_2.SubActivity"
        android:background="#aaf">
     
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="tutorial"
            android:textSize="30dp"
            android:layout_centerInParent="true"/>
     
        <Button
            android:id="@+id/ok_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OK"
            android:layout_alignParentBottom="true"/>
     
        <CheckBox
            android:id="@+id/check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tutorial 다시보지 않기"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>
     
    </RelativeLayout>
    cs

    * 상태를 저장할 CheckBox 값에 의해 tutorial을 다시 볼지를 정하는 예제.


     Activity (Main) 

     

    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
    public class MainActivity extends AppCompatActivity {
     
        Button ok_btn;
        CheckBox check;
     
        SharedPreferences pref;
        boolean isTutorial;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            pref = PreferenceManager.getDefaultSharedPreferences( MainActivity.this);
            isTutorial = pref.getBoolean("tutorial"false);
     
            if(isTutorial)
                startIntent();
     
            ok_btn = (Button)findViewById(R.id.ok_btn);
            check = (CheckBox)findViewById(R.id.check);
     
            ok_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
     
                    SharedPreferences.Editor editor = pref.edit();
                    editor.putBoolean("tutorial", check.isChecked());
                    editor.commit();
     
                    startIntent();
                }
            });
        }//onCreate()
     
        private void startIntent()
        {
            Intent intent = new Intent(MainActivity.this, SubActivity.class);
     
            intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
     
            startActivity(intent);
            finish();
        }
    }
    cs

    * key값이 tutorial 인 boolean 값을 저장한다.


     Layout (Sub) 

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_sub"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.sin.sample_2.SubActivity">
     
        <Button
            android:id="@+id/tutorial_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="tutorial 다시보기"
            android:layout_alignParentBottom="true"/>
     
    </LinearLayout>
    cs

    * tutorial 다시보지 않기를 체크후 닫았을 경우, 다시 tutorial을 표시 할수 있도록 하기 위해 추가.


    Activity (Sub)

     

    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
    public class SubActivity extends AppCompatActivity {
     
        Button tutorial_btn;
        SharedPreferences pref;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sub);
     
            pref = PreferenceManager.getDefaultSharedPreferences(SubActivity.this);
     
            tutorial_btn = (Button)findViewById(R.id.tutorial_btn);
            tutorial_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(SubActivity.this, MainActivity.class);
                    SharedPreferences.Editor editor = pref.edit();
     
                    editor.putBoolean("tutorial"false);
                    editor.commit();
     
                    startActivity(intent);
                }
            });
     
        }// onCreate()
    }
    cs

    * Intent로 화면 전환만 할경우, 기존에 저장된 값으로 인해 tutorial이 표시 되지 않음.

    * 다시 보기를 할경우 key값이 tutorial 인 boolean 값을 false로 하여 다시 표시 하도록 변경.



    3. 결과화면


     



    'Develpment > Android Using Sample' 카테고리의 다른 글

    [14] Canvas  (0) 2020.09.13
    [12] 앱종료 확인 (Handler 응용)  (0) 2020.09.13
    [11] Handler  (0) 2020.09.13
    [10] Intent  (0) 2020.09.13
    [9] Rating Bar  (0) 2020.09.13

    댓글

Designed by Tistory.