IT/Android

2. Activity Life Cycle(Activity 수명 주기)

바바옄 2015. 6. 22. 14:20
반응형

현재 사용중인 app의 데이터가 새로운 app을 호출함으로써 사라지는 데이터를 복원하기 위한 방법 

 

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.ktds.mylifecycle2;
 
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.Toast;
 
 
public class MainActivity extends ActionBarActivity {
 
    private static final String PREF_ID = "PREF_ID";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    // 설정 정보에 저장한 값을 읽어와서 복원
    public void restoreFromSaveState(){
        SharedPreferences myPref = getSharedPreferences(PREF_ID, Activity.MODE_PRIVATE);
        if(myPref != null && myPref.contains("txtMsg")){
            String myData = myPref.getString("txtMsg","");
            Toast.makeText(this,"복원함(txtMsg):"+myData, Toast.LENGTH_SHORT).show();
        }
    }
 
    // 설정 정보에 값을 저장함
    public void saveCurrentState(){
 
        SharedPreferences myPref = getSharedPreferences(PREF_ID, Activity.MODE_PRIVATE);
        SharedPreferences.Editor myEditor = myPref.edit();
        myEditor.putString("txtMsg","내 이름은 성규");
        myEditor.commit();
 
        Toast.makeText(this,"저장함(txtMsg) : 내 이름은 성규", Toast.LENGTH_SHORT).show();
        Log.d("Lifecycle","저장함 (txtMsg) : 내 이름은 성규");
 
    }
 
    // 설정 정보에 저장한 값을 삭제함
    public void clearMyPref(){
 
        SharedPreferences myPref = getSharedPreferences(PREF_ID, Activity.MODE_PRIVATE);
        SharedPreferences.Editor myEditor = myPref.edit();
        myEditor.clear();
        myEditor.commit();
 
        Toast.makeText(this,"삭제함(txtMsg) : 내 이름은 성규", Toast.LENGTH_SHORT).show();
        Log.d("Lifecycle""삭제함 (txtMsg) : 내 이름은 성규");
    }
 
    protected void onResume(){
        super.onResume();
 
        restoreFromSaveState();
 
        Toast.makeText(this,"onResume() 호출 됨",Toast.LENGTH_SHORT).show();
        Log.d("LifeCycle","onResume()");
    }
 
    protected void onPause(){
        super.onPause();
 
        saveCurrentState();
 
        Toast.makeText(this,"onPause() 호출 됨",Toast.LENGTH_SHORT).show();
        Log.d("LifeCycle","onPause()");
    }
 
    protected void onDestroy(){
        super.onDestroy();
 
        clearMyPref();
 
        Toast.makeText(this,"onDestroy() 호출 됨",Toast.LENGTH_SHORT).show();
        Log.d("LifeCycle","onDestroy()");
    }
 
 
}
 
cs

 

 

 

     

 

 

     

 

반응형

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

Receiver  (0) 2015.06.23
2. Activity를 이용한 계산기  (0) 2015.06.22
1. Activity Life Cycle(Activity 수명 주기) 및 Log 남기기  (0) 2015.06.22
1. Activity를 이용한 계산기  (0) 2015.06.22
Layout  (0) 2015.06.22