Activity 생명주기 UNIT 13 로봇 SW 콘텐츠 교육원 조용수
학습 목표 Logcat 액티비티 생명주기를 설명할 수 있다. 현재 상태를 저장할 수 있다. 2
로그캣 Window > Show View > Other… 3
로그캣 4 로그 레벨 verbose, debug, info, warn, error, assert 저장로그 모두 삭제일시 멈춤
로그 레벨 verbose – 배포용 패키지 만들 때 컴파일 단계에서 빠짐 debug – 컴파일은 되지만 런타임 시에 빠짐 info warn error assert – 절대로 일어나서는 안 되는 치명적인 오류 5
로그캣 6 필터 추가 필터 삭제필터 편집
Log public static int v (String tag, String msg) public static int d (String tag, String msg) public static int i (String tag, String msg) public static int w (String tag, String msg) public static int e (String tag, String msg) public static int wtf (String tag, String msg) 7
Log 8 public class MainActivity extends RobotActivity { private Device leftWheelDevice; private Device rightWheelDevice; private MicThread thread; private static final String TAG = protected void onCreate(Bundle savedInstanceState) { Log.v(TAG, "onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); thread = new MicThread(); thread.start(); protected void onDestroy() { Log.v(TAG, "onDestroy"); super.onDestroy(); if(thread != null) thread.cancel(); }
Log 9 private class MicThread extends Thread { private static final int FREQ = 44100; private static final int CHANNEL = AudioFormat.CHANNEL_IN_MONO; private static final int ENCODING = AudioFormat.ENCODING_PCM_16BIT; private boolean running = public void run() { Log.v(TAG, "thread started");... while(running) {... }... Log.v(TAG, "thread finished"); } void cancel() { Log.v(TAG, "cancel"); running = false; }
액티비티 생명주기 10 액티비티 상태 – 실행 – 일시 정지 – 정지
액티비티 생명주기 11
액티비티 생명주기 화면이 회전하는 경우 12 onCreate 회전 onStart onResume onSaveInstanceState onPause onStop onDestroy onRestoreInstanceState onCreate onStart onResume
액티비티 생명주기 Home 키 누른 후 앱 다시 실행 13 onCreate Home 키 onStart onResume onSaveInstanceState onPause onStop onRestart onStart onResume 다시 실행
액티비티 생명주기 Back 키를 누른 경우 14 onCreate Back 키 onStart onResume finish onPause onStop onDestroy onBackPressed
액티비티 생명주기 15 public class MainActivity extends Activity { private static final String TAG = protected void onCreate(Bundle savedInstanceState) { Log.v(TAG, "onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); protected void onDestroy() { Log.v(TAG, "onDestroy"); super.onDestroy(); }
액티비티 생명주기 protected void onResume() { Log.v(TAG, "onResume"); super.onResume(); protected void onPause() { Log.v(TAG, "onPause"); super.onPause(); protected void onStart() { Log.v(TAG, "onStart"); super.onStart(); protected void onStop() { Log.v(TAG, "onStop"); super.onStop(); }
액티비티 생명주기 protected void onRestart() { Log.v(TAG, "onRestart"); super.onRestart(); protected void onSaveInstanceState(Bundle outState) { Log.v(TAG, "onSaveInstanceState"); super.onSaveInstanceState(outState); protected void onRestoreInstanceState(Bundle savedInstanceState) { Log.v(TAG, "onRestoreInstanceState"); super.onRestoreInstanceState(savedInstanceState); }
액티비티 생명주기 public void onBackPressed() { Log.v(TAG, "onBackPressed"); super.onBackPressed(); public void finish() { Log.v(TAG, "finish"); super.finish(); }
현재 상태 저장 19 private Button protected void onCreate(Bundle savedInstanceState) { Log.v(TAG, "onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button); button.setOnClickListener(new OnClickListener() public void onClick(View v) { button.setText(R.string.clicked); } }); }
현재 상태 저장 protected void onSaveInstanceState(Bundle outState) { Log.v(TAG, "onSaveInstanceState"); super.onSaveInstanceState(outState); String text = button.getText().toString(); outState.putString("ButtonText", text); protected void onRestoreInstanceState(Bundle savedInstanceState) { Log.v(TAG, "onRestoreInstanceState"); super.onRestoreInstanceState(savedInstanceState); button.setText(savedInstanceState.getString("ButtonText")); }
번들 (Bundle) –void putBoolean(String key, boolean value) –void putBooleanArray(String key, boolean[] value) –void putByte(String key, byte value) –void putByteArray(String key, byte[] value) –void putChar(String key, char value) –void putCharArray(String key, char[] value) –void putShort(String key, short value) –void putShortArray(String key, short[] value) –void putInt(String key, int value) –void putIntArray(String key, int[] value) –void putLong(String key, long value) –void putLongArray(String key, long[] value) –void putFloat(String key, float value) –void putFloatArray(String key, float[] value) –void putDouble(String key, double value) –void putDoubleArray(String key, double[] value) –void putString(String key, String value) –void putStringArray(String key, String[] value) –void putCharSequence(String key, CharSequence value) –void putCharSequenceArray(String key, CharSequence[] value) –… 21 Bundle Object
번들 (Bundle) –boolean getBoolean(String key) –byte getByte(String key) –char getChar(String key) –short getShort(String key) –int getInt(String key) –long getLong(String key) –float getFloat(String key) –double getDouble(String key) –String getString(String key) –CharSequence getCharSequence(String key) –… 22 –boolean getBoolean(String key, boolean defaultValue) –byte getByte(String key, byte defaultValue) –char getChar(String key, char defaultValue) –short getShort(String key, short defaultValue) –int getInt(String key, int defaultValue) –long getLong(String key, long defaultValue) –float getFloat(String key, float defaultValue) –double getDouble(String key, double defaultValue) –String getString(String key, String defaultValue) –CharSequence getCharSequence(String key, CharSequence defaultValue) –… –byte[] getByteArray(String key) –char[] getCharArray(String key) –short[] getShortArray(String key) –int[] getIntArray(String key) –long[] getLongArray(String key) –float[] getFloatArray(String key) –double[] getDoubleArray(String key) –String[] getStringArray(String key) –CharSequence[] getCharSequenceArray(String key) –…
리소스 23 layout-land layout-port
리소스 폴더 MCC(mobile country code) and MNC(mobile network code): mcc310-mnc004 Language and region: en, fr, en-rUS, fr-rFR, fr-rCA, ko Layout direction: ldrtl, ldltr Smallest width: sw320dp, sw720dp Available width: w720dp, w1024dp Available height: h720dp, h1024dp Screen size: small, normal, large, xlarge Screen aspect: long, notlong Screen orientation: port, land UI mode: car, desk, television, appliance Night mode: night, notnight Screen pixel density: ldpi, mdpi, hdpi, xhdpi, xxhdpi, nodpi, tvdpi Touchscreen type: notouch, finger Keyboard availability: keysexposed, keyshidden, keyssoft Primary text input method: nokeys, qwerty, 12key Navigation key availability: navexposed, navhidden Primary non-touch navigation method: nonav, dpad, trackball, wheel Platform version: v3, v7 24
configChanges 25 onCreate 회전 onStart onResume onConfigurationChanged <activity android:name="com.example.unit16.MainActivity" android:configChanges="orientation|screenSize" public void onConfigurationChanged(Configuration newConfig) { Log.v(TAG, "onConfigurationChanged"); super.onConfigurationChanged(newConfig); }