투케이2K

689. (Android/Java) Application 애플리케이션 클래스 ActivityLifecycleCallbacks 상속 후 Activity 액티비티 라이프 사이클 감지 본문

Android

689. (Android/Java) Application 애플리케이션 클래스 ActivityLifecycleCallbacks 상속 후 Activity 액티비티 라이프 사이클 감지

투케이2K 2023. 11. 14. 19:49

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[소스 코드]

package com.example.javaproject;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class A_Application extends Application implements Application.ActivityLifecycleCallbacks {


    /**
     * // --------------------------------------------------------------------------------------
     * TODO [클래스 설명]
     * // --------------------------------------------------------------------------------------
     * 1. 애플리케이션 프로세스 관리 클래스
     * // --------------------------------------------------------------------------------------
     * 2. AndroidManifest.xml 파일에서 application 부분에 등록 필요
     *
     *    <application
     *       android:name="com.example.testapp.A_Application"
     *    </application>
     * // --------------------------------------------------------------------------------------
     * */





    /**
     * // --------------------------------------------------------------------------------------
     * // TODO [빠른 로직 찾기 : 주석 로직 찾기]
     * // --------------------------------------------------------------------------------------
     * // [SEARCH FAST] : []
     * // --------------------------------------------------------------------------------------
     *
     * // --------------------------------------------------------------------------------------
     *
     * // --------------------------------------------------------------------------------------
     *
     * // --------------------------------------------------------------------------------------
     *
     * // --------------------------------------------------------------------------------------
     * */





    // -----------------------------------------------------------------------------------------
    // TODO [전역 변수 선언]
    // -----------------------------------------------------------------------------------------
    public static final String ACTIVITY_NAME = "A_Application";





    // -----------------------------------------------------------------------------------------
    // TODO [프로세스 생명주기 관리]
    // -----------------------------------------------------------------------------------------
    @Override
    protected void attachBaseContext(Context base) {
        try {
            super.attachBaseContext(base);
            // ===============================================================
            S_Log._D_(S_FinalMsg.LOG_Application_attachBaseContext, null);
            // ===============================================================
        }
        catch (Exception e){
            S_Log._printStackTrace_(null, S_FinalMsg.LOG_BUG_STATE, null, e);
        }
    }





    // -----------------------------------------------------------------------------------------
    // TODO [프로세스 생명주기 관리]
    // -----------------------------------------------------------------------------------------
    @Override
    public void onCreate() {
        try {
            super.onCreate();
            // ===============================================================
            // [앱 최초 실행 시 로그 파일 초기화]
            // ===============================================================
            S_Log._F_(getApplicationContext(), ACTIVITY_NAME + " : " + S_FinalMsg.LOG_Application_onCreate  + " : " + C_Util.getSourceCodeLine(), null);

            
            // ---------------------------------------------------------------
            // [액티비티 라이프 사이클 상태 감지 등록]
            // ---------------------------------------------------------------
            try {
                registerActivityLifecycleCallbacks(this);
            }
            catch (Exception e){
                S_Log._printStackTrace_(null, S_FinalMsg.LOG_BUG_STATE, null, e);
            }


            // ---------------------------------------------------------------
            // [작업 태스크 날림 이벤트 서비스 개시]
            // ---------------------------------------------------------------
            try {
                startService(new Intent(getApplicationContext(), S_ProgramTaskService.class));
            }
            catch (Exception e){
                S_Log._printStackTrace_(null, S_FinalMsg.LOG_BUG_STATE, null, e);
            }
            // ---------------------------------------------------------------


            // ---------------------------------------------------------------
            // [애플리케이션 시작 시 데이터 초기화 실시]
            // ---------------------------------------------------------------
            // S_Preference.proccessCreateClear(getApplicationContext());
            // ---------------------------------------------------------------
        }
        catch (Exception e){
            S_Log._printStackTrace_(null, S_FinalMsg.LOG_BUG_STATE, null, e);
        }
    }





    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 onCreate 확인]
    // -----------------------------------------------------------------------------------------
    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
        try {
            S_Log._D_(S_FinalMsg.LOG_Application_Activity_Lifecycle + " :: " + String.valueOf(activity.getLocalClassName()) + " :: Created", null);
        }
        catch (Exception e){}
    }





    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 onStarted 확인]
    // -----------------------------------------------------------------------------------------
    @Override
    public void onActivityStarted(@NonNull Activity activity) { }





    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 onResumed 확인]
    // -----------------------------------------------------------------------------------------
    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        try {
            S_Log._W_(S_FinalMsg.LOG_Application_Activity_Lifecycle + " :: " + String.valueOf(activity.getLocalClassName()) + " :: Resumed", null);
        }
        catch (Exception e){}
    }





    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 onPaused 확인]
    // -----------------------------------------------------------------------------------------
    @Override
    public void onActivityPaused(@NonNull Activity activity) {
        try {
            S_Log._E_(S_FinalMsg.LOG_Application_Activity_Lifecycle + " :: " + String.valueOf(activity.getLocalClassName()) + " :: Paused", null);
        }
        catch (Exception e){}
    }





    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 onStopped 확인]
    // -----------------------------------------------------------------------------------------
    @Override
    public void onActivityStopped(@NonNull Activity activity) { }





    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 InstanceState 확인]
    // -----------------------------------------------------------------------------------------
    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) { }





    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 Destroyed 확인]
    // -----------------------------------------------------------------------------------------
    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        try {
            S_Log._E_(S_FinalMsg.LOG_Application_Activity_Lifecycle + " :: " + String.valueOf(activity.getLocalClassName()) + " :: Destroyed", null);
        }
        catch (Exception e){}
    }


} // TODO [클래스 종료]
 

[결과 출력]

 

D///===========//: ================================================
I/: [LOG :: CLASS PLACE :: com.example.javaproject.A_Application.onActivityCreated(A_Application.java:157)]
I/: ----------------------------------------------------
I/: [LOG :: NOW TIME :: 2023-11-14 08:11:07 화요일]
I/: ----------------------------------------------------
I/: [LOG :: DESCRIPTION :: 애플리케이션 >> 액티비티 :: 라이프 사이클 감지 :: A_Intro :: Created]
D///===========//: ================================================



D///===========//: ================================================
I/: [LOG :: CLASS PLACE :: com.example.javaproject.A_Intro.onCreate(A_Intro.java:227)]
I/: ----------------------------------------------------
I/: [LOG :: NOW TIME :: 2023-11-14 08:11:07 화요일]
I/: ----------------------------------------------------
I/: [LOG :: DESCRIPTION :: 액티비티 시작 수행]
I/: ----------------------------------------------------
I/: [LOG :: 2023-11-14 08:11:07:613]
D///===========//: ================================================

 

반응형
Comments