본문 바로가기
매일코딩/안드로이드

[안드로이드 기초] 액티비티의 수명 주기

by 인생여희 2017. 8. 16.
반응형

액티비티의 수명 주기


 

#핵심


 앱 실행할 때 동작하는 메서드 순서 create-> start -> resume(복구되기 바로전 실행됨)

 앱 종료할 때 동작하는 메서드  순서 pause(정지되기 바로전 실행됨) -> stop ->destroy



#개념



#소스

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
84
85
86
87
package me.happygate.myapplication111;
 
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
 
        Toast.makeText(this,"onCreate() start",Toast.LENGTH_SHORT).show();
        
        //버튼을 클릭하면
        Button button =(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //종료
                finish();
 
            }
        });
 
    }
 
    @Override
    protected void onStart() {
        super.onStart();
        Toast.makeText(this,"onStart() start",Toast.LENGTH_SHORT).show();
    }
 
    @Override
    protected void onStop() {
        super.onStop();
        Toast.makeText(this,"onStop() start",Toast.LENGTH_SHORT).show();
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Toast.makeText(this,"onDestroy() start",Toast.LENGTH_SHORT).show();
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        Toast.makeText(this,"onPause() start",Toast.LENGTH_SHORT).show();
        //정지되기 전에 저장시킬 데이터를 SharedPreferences 를 통해 만들어 준다.
        SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor =pref.edit();
        editor.putString("name","my name is kang");
        editor.commit();
    }
 
    @Override
    protected void onRestart() {
        super.onRestart();
        Toast.makeText(this,"onRestart() start",Toast.LENGTH_SHORT).show();
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(this,"onResume() start",Toast.LENGTH_SHORT).show();
 

        //앱이 복구가 될때 resume에서 저장한 데이터를 꺼내준다.
        SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
        if(pref != null){

//name에 아무것도 없으면 "" 넣어준다.

            String name = pref.getString("name","");
            Toast.makeText(this,"복구된 이름:"+name,Toast.LENGTH_SHORT).show();
        }
    }
 
 
    // 앱 실행할 때 동작하는 순서 create-> start -> resume(복구되기 바로전)
    // 앱 종료할 때 동작하는 순서 pause(정지되기 바로전) -> stop ->destroy
}
 
 
cs


반응형

댓글