사용자가 앱을 실행, 나오기, 다시 접근할 때
액티비티 인스턴스는 라이프사이클의 각각의 상태에
놓인다.
액티비티가 처음으로 시작할 때 시스템의 전면으로 나와
사용자의 사용자 포커스를 받는다.
이 과정에서 안드로이드 시스템은 사용자에 의해 설정된 유저 인터페이스
와 다른 컴퍼넌트들의 메스드를 호출한다.
사용자는 다른 액티비티 또는 다른 앱을 실행할 경우
해당 액티비티는 백그라운드로 이동하여 해당 액티비티의 또 다른
라이프 사이클 함수 그룹을 호출하여 처리한다.
액티비티 시작하기
main() 메서드에서 개시되는 다른 프로그래밍 패러다임과 달리
안드로이드는 라이프 사이클의 특정 단계에 대응되는 콜백함수를
호출함으로써 액티비티 인스턴스의 코드를 초기화한다.
라이프사이클 콜백 이해하기
onCreate() -> Created -- onStart() -> Started(visible) -- onResume() -> Resumed(visible)
Resumed(visible) -- onPause() -> Paused(partially visible)
Paused -- onResume() -> Resumed
Paused -- onStop() -> Stopped(hidden)
Stopped -- onStart() -> Started
Stopped -- onDestroy() -> Destroyed
액티비티 라이프사이클 함수들의 역활
-사용자가 앱을 사용중 전화 수신시 또는 다른 앱으로 전환될 경우 종료되지 않게 한다.
-시스템 자원의 효율적 관리
-사용자 어플을 떠나서 다시 복귀해 어플의 전개과정을 이어간다.
-스크린의 랜드스케이프와 포트레이트 회전시 어플의 크래시 방지와 프로그래스를 잃지 않게 함.
액티비티의 정적 상태
-Resumed
: 액티비티가 전면에 배치되고 사용자와 상호작용할 수 있음
-Paused
: 액티비티가 다른 액티비티에 의해 부분적으로 가려지며 사용자와 상호작용이
중지된다.
-Stopped
: 액티비티는 완전히 숨겨져 백그라운드로 놓인며 해당 인스턴스와 멤버변수같은 상태정보들이 저장된다.
론처 액티비티 지정
사용자가 홈화면에서 앱아이콘을 클릭하면 시스템은 론처 또는 메인으로 지정된
액티비티의 onCreate()를 호출한다.
메인 액티비티는 프로젝트의 루트 디렉토리에서 AndoridManifest.xml에서 정의하며
MAIN action과 LAUNCHER category를 포함하는 <intent-filter>를 선언함으로써
이루어진다.
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity
새 인스턴스 생성
TextView mTextView; // Member variable for text view in the layout
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the user interface layout for this Activity
// The layout file is defined in the project res/layout/main_activity.xml file
setContentView(R.layout.main_activity);
// Initialize member TextView so we can manipulate it later
mTextView = (TextView) findViewById(R.id.text_message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// For the main activity, make sure the app icon in the action bar
// does not behave as a button
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
}
}
액티비티 종료하기
@Override
public void onDestroy() {
super.onDestroy(); // Always call the superclass
// Stop method tracing that the activity started during onCreate()
android.os.Debug.stopMethodTracing();
}
액티비티의 중지와 재개
@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
// Release the Camera because we don't need it when paused
// and other activities might need to use it.
if (mCamera != null) {
mCamera.release()
mCamera = null;
}
}
액티비티 정지와 재시작
-시나리오
사용자가 Recent Apps window를 열어 다른 앱으로 이동한다. 현재의 사용자 앱은
전면에 있는 것은 stop된다.
@Override
protected void onStop() {
super.onStop(); // Always call the superclass method first
// Save the note's current draft, because the activity is stopping
// and we want to be sure the current note progress isn't lost.
ContentValues values = new ContentValues();
values.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText());
values.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle());
getContentResolver().update(
mUri, // The URI for the note to update.
values, // The map of column names and new values to apply to them.
null, // No SELECT criteria are used.
null // No WHERE columns are used.
);
}
@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
// Get the Camera instance as the activity achieves full user focus
if (mCamera == null) {
initializeCamera(); // Local method to handle camera init
}
}