액티비티 생명주기(Activity Life Cycle) 는 총 6단계를 거치며, 상황에 따라 1단계가 더 추가될 수 있다.
6단계는 시간의 순서에 따라 생성, 시작, 활성화, 비활성화, 중비, 소멸이며,
애플리케이션이 비활성화되었다가 활성화되는 순간 재시작을 거치게 되어 사실상 7단계로 구성된다. (재시작되지 않은채 소멸될수 있다. )

처음 실행시 OnCreate(), onStart(), onResume()순으로 빠르게 호출이 실행된다.
이것은 액티비티가 처음으로 실행될 때이며 테스크가 백그라운드로 숨었다가 다시 실행될 때에는 onStart() onResume() 메서드를 호출하고, onCreate()는 생략된다.
onPause()는 액티비티가 비활성화되어 화면에서 사라지기 직전을 나타낸다.
onStop() 은 액티비티가 화면상에서 완전히 사라져버릴때 호출된다. onPause() 뒤에 불리며 시스템메모리가 부족한 경우 onStop()은 호출되지 않는다.

대부분의 경우 액티비티가 화면상에서 사라지는 순간 onPause(), onStop()순으로 연속 호출되어 두 메서드 사이의 경계를 구분하기가 어려울 수 있는데, onStop()의 호출없이 onPause() 메서드만 호출되었다가 활성화되었을때 onResume()으로 돌아가는 경우는 현재 액티비티 앞에 반투명 액티비티가 존재하는 경우이다.

* 일반적인 실행 종료 주기


#  두개의 액티비티가 존재할 경우
* 최초 실행
사용자 삽입 이미지사용자 삽입 이미지
*  A액티비티 택스트 클릭시 B 액티비티 실행
사용자 삽입 이미지사용자 삽입 이미지
* 취소 버튼으로 B 액티비티 취소
사용자 삽입 이미지사용자 삽입 이미지
* A 액티비티 까지 전부 종료
사용자 삽입 이미지사용자 삽입 이미지

* ActivityLifeCycle.java
package com.froglamb.activity_life_cycle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class ActivityLifeCycle extends Activity {
   
    private String TAG = "ActivityLifeCycle";
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i(TAG, "onCreate()");   
       
        //SecondaryActivity 추가
        TextView text_view = (TextView) findViewById(R.id.text_view);
        text_view.setOnClickListener(
                new OnClickListener() {
                    public void onClick(View v) {
                        Intent intent = new Intent(ActivityLifeCycle.this,
                                SecondActivity.class);
                        startActivity(intent);
                    }
                });
    }
   
    @Override
    public void onStart(){
        super.onStart();
       
        Log.i(TAG, "onStart()");
    }
   
    @Override
    public void onStop(){
        super.onStop();
       
        Log.i(TAG, "onStop()");
    }
   
    @Override
    public void onResume(){
        super.onResume();
       
        Log.i(TAG, "onResume()");
    }
   
    @Override
    public void onRestart(){
        super.onRestart();
       
        Log.i(TAG, "onRestart()");
    }
   
    @Override
    public void onDestroy(){
        super.onDestroy();
       
        Log.i(TAG, "onDestroy()");
    }
   
    @Override
    public void onPause(){
        super.onPause();
       
        Log.i(TAG, "onPause()");
    }
       
}

* SecondActivity.java
package com.froglamb.activity_life_cycle;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class SecondActivity extends Activity{
    private String TAG = "ActivityLifeCycle";
   
    @Override
    public void onCreate(Bundle savedInstanceBundle){
        super.onCreate(savedInstanceBundle);
        Log.i(TAG, "SecondActivity/onCreate()");
       
        TextView textview = new TextView(this);
        textview.setText("SecondActivity !!!");
        setContentView(textview);
    }
}


* Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.froglamb.activity_life_cycle"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ActivityLifeCycle"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="SecondActivity" android:theme="@android:style/Theme.Translucent" />

    </application>
    <uses-sdk android:minSdkVersion="8" />

</manifest>

* main.xml - Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:id="@+id/text_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>


크리에이티브 커먼즈 라이센스
Creative Commons License
이올린에 북마크하기(0) 이올린에 추천하기(0)

Posted by 홍반장

2010/11/08 12:10 2010/11/08 12:10
, , ,
Response
No Trackback , No Comment
RSS :
http://tcbs17.cafe24.com/tc/rss/response/5629


블로그 이미지

- 홍반장

Archives

Recent Trackbacks

Calendar

«   2024/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            
Statistics Graph

Site Stats

Total hits:
175542
Today:
68
Yesterday:
41