본문 바로가기
안드로이드/개발자 일상

안드로이드 생명주기 (LifeCycle)

by 차누감 2020. 4. 8.

 

생명주기에 관한 자세한 사항은 안드로이드 개발자 사이트를 참고해주세요.

https://developer.android.com/guide/components/activities/activity-lifecycle?hl=ko

 

Activity 수명 주기에 대한 이해  |  Android 개발자  |  Android Developers

An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface…

developer.android.com

Activity 생명 주기 그림

간략한 설명

onCreate - 액티비티 만들어질때 딱 1번 호출

 

onStart - 액티비티 시작 할때 호출 (또는 화면이 가려졌다가 다시 보일때 호출)  

 

onResume - 액티비티가 화면에 확실히 보일때 호출, foreGround시 (우리눈에 화면이 다 보일때)

 

onPause - 액티비티가 화면에 살짝 가려지면 호출, focus를 잃은 상태

                (문자가 와서 상단에 알림이 떴을때, 화면은 보이지만 focus는 문자 알림에 가있다.)

 

onStop - 액티비티가 화면에 완전히 안보일때 호출

 

onDestroy - 티비티를 종료하면 호출, finsh() 또는 휴대폰 뒤로가기 버튼으로 앱 종료

 

( onRestart()은 위 그림 참고하세요. )

 

 

<실행 화면> MainActivity 기준 생명 주기 화면입니다. 앱 실행 -> 버튼누름 -> 두번째 화면 닫기 -> 앱 종료

앱 실행 화면이 보이기 까지  onCreate -> onStart-> onResume

버튼을 눌러 화면이 가려지기 까지 onPause -> onStop

뒤로가기로 다시 화면이 보인다면 onStart-> onResume

한번 더 뒤로가기로 MainActivity를 종료한다면 onPause -> onStop -> onDestroy


앱 실행으로 알아보겠습니다.

 

우선 MainActivity 생명주기를 보기 위해

버튼을 누르면 화면을 가리기 위한 SecondActivity를 띄워 확인해봅니다.

 

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
<?xml version="1.0" encoding="utf-8"?>
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MainActivity"
        android:textSize="28sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="second Acivity Call"
        android:layout_marginBottom="12dp"
        android:onClick="click_secondCall"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
 
 

SecondActivity를 만듭니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SecondActivity"
        android:textSize="28sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
 
 

이제 MainActivity에서 생명 주기를 알기 위해 Toast 띄우는 작업 및 버튼 이벤트 작업을 합니다.

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
 
 
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {    // 액티비티 만들어질때 딱 1번 호출
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(this"onCreate", Toast.LENGTH_SHORT).show();
    }// onCreate()..
 
    @Override
    protected void onStart() {                              // 액티비티 시작 할때 여러번 호출 가능
        super.onStart();
        Toast.makeText(this"onStart", Toast.LENGTH_SHORT).show();
    }
 
    @Override
    protected void onResume() {                             // 액티비티가 화면에 확실히 보일때 호출, foreGround시
        super.onResume();
        Toast.makeText(this"onResume", Toast.LENGTH_SHORT).show();
    }
 
    @Override
    protected void onPause() {                              // 액티비티가 화면에 살짝 가려지면 호출, focus를 잃은 상태태
        super.onPause();
        Toast.makeText(this"onPause", Toast.LENGTH_SHORT).show();
    }
 
    @Override
    protected void onStop() {                               // 액티비티가 화면에 완전히 안보일때 호출
        super.onStop();
        Toast.makeText(this"onStop", Toast.LENGTH_SHORT).show();
    }
 
    @Override
    protected void onDestroy() {                            // 액티비티를 종료하면 호출, finsh() 또는 휴대폰 뒤로가기 버튼
        super.onDestroy();
        Toast.makeText(this"onDestroy", Toast.LENGTH_SHORT).show();
    }
 
 
    // 두 번째 화면 호출 버튼
    public void click_secondCall(View view) {
        Intent intent=new Intent(this,SecondActivity.class);
        startActivity(intent);
    }
 
}// MainActivity class..
 
 
 

<실행 화면> MainActivity 기준 생명 주기 화면입니다.

앱 실행 화면이 보이기 까지  onCreate -> onStart-> onResume

버튼을 눌러 화면이 가려지기 까지 onPause -> onStop

뒤로가기로 다시 화면이 보인다면 onStart-> onResume

한번 더 뒤로가기로 MainActivity를 종료한다면 onPause -> onStop -> onDestroy

보기 힘들다면 동영상으로 보세요.

 

댓글