본문 바로가기

Android

Android] Activity 생명 주기

                                     

onCreate( )   - 액티비티 생성시 실행 , 전체 생명주기 중 1번만 발생

 

onStart( )     - 액티비티 실행서 시스템이 호출 , 매우 빠르게 완료

 

onResume( ) - 사용자가 앱과 상호작용, 방해 이벤트 발생 시 일시 중지 상태로 들어가고 onPuase( ) 호출

 

onPause( )   - 사용자가 액티비티를 떠날 때의 일시정지 상태 , 아주 잠깐 실행하여 데이터 저장, 트랜잭션 실행 등 

 

onStop( )     - 중단 상태로 실행이 필요 없고 기능 모두 정지가 가능 , 활동이 다시 시작되면 onRestart( ) 호출

 

onDestroy( ) - 액티비티 소멸 전에 호출, 사용자가 액티비티를 닫거나 finish( ) 호출

 

* 1) 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">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="다른 액티비티로 이동"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

* 2) activity_sub.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Button
        android:id="@+id/rtbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이전 액티비티로 돌아가기"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

* 3) MainActivity.java

package com.example.life;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private static String TAG = "MainActivity";

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

        Button button = (Button)findViewById(R.id.btn1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(),SubActivity.class);
                startActivity(intent);
            }
        });

        Log.d(TAG,"onCreate");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG,"onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG,"onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG,"onPause");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG,"onRestart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG,"onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }


}

* 4) SubActivity.java

package com.example.life;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class SubActivity extends AppCompatActivity {

    protected void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);
        setContentView(R.layout.activity_sub);

        Button button = (Button) findViewById(R.id.rtbtn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(intent);
            }
        });
    }

}

 

1. 액티비티 실행 할 경우

 

2. 다른 액티비티로 이동 할 경우

 

3. 다른 액티비티 이동 후 다시 돌아올 경우

 

4. 다른 액티비티 이동 후 '이전 액티비티로 돌아가기' 버튼 클릭 시

 

5. 홈 키 클릭시

6. 어플리케이션 종료시 

 

 

프로세스 액티비티 상태
포그라운드 onCreate( ),
onStart( ),
onResume( )
백그라운드(포커스 상실) onPause( )
백그라운드(보이지 않음) onStop( )
삭제 onDestroy( )


Calendar
«   2025/03   »
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
Tags
더보기
Archives
Visits
Today
Yesterday