https://goni95.tistory.com/173
๐Android์ ๋ชจ๋ ๊ฒ ๊ฐ๋
Android ๋ชฉ ์ฐจ Android Android SDK Platform Architecture App Manifest Android Context App Components Activity์ Fragment ์๋ช ์ฃผ๊ธฐ Activity์ Fragment ์ฐจ์ด LayoutInflater ANR View ๋ฐ ViewGroup ํ๋ฉด..
goni95.tistory.com
1๏ธโฃ App Manifest
์๋๋ก์ด๋ ์์คํ
์ด ์ดํ๋ฆฌ์ผ์ด์
์ ์คํํ๊ธฐ ์ ํ๋ณดํด์ผํ๋ ์ ํ๋ฆฌ์ผ์ด์
์ ๋ํ ํ์ ์ ๋ณด๊ฐ ๊ธฐ์ ๋ ๋ช
์ธ์ ์
๋๋ค.
๋งค๋ํ์คํธ ํ์ผ์๋ ์ฑ์ ํจํค์ง ๋ช
, ์ฑ์์ ์ฌ์ฉ๋๋ ์ปดํฌ๋ํธ, ์ฑ๊ณผ ์ํธ์์ฉํ ๋ ํ์ํ ๊ถํ, ์ฑ์์ ์๊ตฌํ๋ ํ๋์จ์ด์ ์ํํธ์จ์ด์ ํน์ง ๋ฑ์ด ์ ์๋์ด ์์ต๋๋ค.
๋งค๋ํ์คํธ์ ๊ตฌ์กฐ
<manifest> : ํจํค์ง
<uses-permission> : ๊ถํ
<application> : ์ปดํฌ๋ํธ
<uses-feature> : ๊ธฐ๊ธฐ ํธํ์ฑ
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ํจํค์ง ๋ช
">
//1. package : "๋น๋ ๋๊ตฌ๊ฐ ํจํค์ง ๋ช
์ ์ฌ์ฉํด ๋งค๋ํ์คํธ ํ์ผ์ ์ ์ธ๋ ํด๋์ค ์ด๋ฆ์ ํ์ธํ๊ธฐ ๋๋ฌธ์ java์ namespace์ ์ผ์น ์์ผ์ผ ํฉ๋๋ค."
<uses-permission android:name="android.permission.INTERNET"/>
//2. permission : ์ ํ๊ฑธ๊ธฐ, ์ธํฐ๋ท ๋ฑ ์ดํ๋ฆฌ์ผ์ด์
์ด ์คํ๋์ด ์ํธ์์ฉํ ๋ ํ์ํ ๊ถํ์ ์ ์
<application
android:name=".CalIngredientFoodApplication"
//3. application name : ์ดํ๋ฆฌ์ผ์ด์
์ ์ํด ๊ตฌํ๋ ์ดํ๋ฆฌ์ผ์ด์
์๋ธํด๋์ค์ ์ด๋ฆ / ์ดํ๋ฆฌ์ผ์ด์
ํ๋ก์ธ์ค๊ฐ ์์๋ ๋, ๊ฐ์ฅ ๋จผ์ ๊ฐ์ฒดํ๋๋ฉฐ ์๋ธํด๋์ค๊ฐ ์์ด๋ ์๋๋ก์ด๋๋ ๋ฒ ์ด์ค ์ดํ๋ฆฌ์ผ์ด์
ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉ
android:allowBackup="true"
//4. backup : ๊ตฌ๊ธ ํด๋ผ์ฐ๋์ ์ดํ๋ฆฌ์ผ์ด์
์ ๋ฐ์ดํฐ๋ฅผ ์๋ ๋ฐฑ์
ํ ๊ฒ์ธ์ง์ ์ ๋ฌด
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
//5. icon๊ณผ label : ํด๋น ์ดํ๋ฆฌ์ผ์ด์
๋ํด ์ฌ์ฉ์์๊ฒ ํ์ํ๊ธฐ ์ํ icon๊ณผ label์ ์ค์
android:supportsRtl="true"
//6. supportsRtl : ์ฐ์ธก๋ถํฐ ํ
์คํธ๋ฅผ ์ฝ๋ ๋๋ผ์ผ ๊ฒฝ์ฐ true๋ก ์ค์ / ์ฐ๋ฆฌ๋๋ผ์์ true๋ก ์ค์ ํด๋ ๊ฒฐ๊ณผ๋ ๋์ผ
android:theme="@style/Theme.CalIngredientFood"
//7. AppTheme : ์ดํ๋ฆฌ์ผ์ด์
์์ ๊ธฐ๋ณธ ํ
๋ง๋ก ์ฌ์ฉํ ํ
๋ง๋ฅผ ์ค์
android:usesCleartextTraffic="true">
//8. usesCleartextTraffic : ์ดํ๋ฆฌ์ผ์ด์
์ด ์ผ๋ฐ ํ
์คํธ HTTP์ ๊ฐ์ ์ผ๋ฐ ํ
์คํธ ๋คํธ์ํฌ ํธ๋ํฝ์ ์ฌ์ฉํ ๊ฒ์ธ์ง ์ฌ๋ถ / API 28 ์ด์ ๋ถํฐ๋ false๊ฐ ๊ธฐ๋ณธ๊ฐ
<activity android:name="sang.gondroid.calingredientfood.presentation.MainActivity"
android:windowSoftInputMode="adjustPan">
//9. activity : ์ดํ๋ฆฌ์ผ์ด์
์ ์ฌ์ฉํ Activity์ ๋ํ ์ ๋ณด / manifest ํ์ผ์ ์ ์ธ๋์ง ์์ ์ปดํฌ๋ํธ๋ ์คํ๋์ง ์๋๋ค.
//10. windowSoftInputMode : ํค๋ณด๋ ํ์ฑํ ์ ๋ชจ๋
adjustPan : ํค๋ณด๋ ํ์ฑํ ์ UI ํ๋ฉด์ด ์กฐ์ ๋๋ ๊ฒ์ ๋ง์
<intent-filter>
//11. intent filter : activity, service, broadcast receiver๊ฐ ์๋ตํ ์ ์๋ Intent ์ ํ์ ์ง์
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
//12. action : ์ธํ
ํธ ํํฐ์ ์์
์ ์ถ๊ฐ
//13. category : ์ธํ
ํธ ํํฐ์ ์นดํ
๊ณ ๋ฆฌ ์ด๋ฆ์ ์ถ๊ฐ
</intent-filter>
</activity>
</application>
<uses-feature android:name="android.hardware.sensor.compass"
android:required="true" />
//4. ๊ธฐ๊ธฐ ํธํ์ฑ : ์ฑ์์ ์๊ตฌํ๋ ํ๋์จ์ด/์ํํธ์จ์ด ๊ธฐ๋ฅ ๋ฐ ํธํ๋๋ ๊ธฐ๊ธฐ์ ํ์ ๋ฑ๋ก
// ๋์นจ๋ฐ ์ผ์๋ฅผ ๋ฑ๋กํ์ผ๋ฉฐ, ํด๋น ์ผ์๊ฐ ํญ์ฌ๋์ง ์์ ๊ธฐ์ข
์ Google Play Store์์ ์ค์น๊ฐ ๋ถ๊ฐ๋ฅ
</manifest>
2๏ธโฃ Context
์์คํ ์ด ๊ด๋ฆฌํ๋ ์ดํ๋ฆฌ์ผ์ด์ ์ ํ์ฌ ์ํ์ ๋ํ ์ ๋ณด์ ์ ๊ทผํ ์ ์๋ ์ถ์ ํด๋์ค๋ก ์กํฐ๋นํฐ์ ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ํ ์ ๋ณด๋ฅผ ์ป๊ธฐ์ํด์ ์ฌ์ฉ๋ฉ๋๋ค.
์กํฐ๋นํฐ๋ ์ดํ๋ฆฌ์ผ์ด์ ์ด ํ์ฌ ์คํ ์ค์ธ ํ๊ฒฝ์ ๋ํ ํธ๋ค๊ณผ๋ ๊ฐ์ต๋๋ค. ์กํฐ๋นํฐ ๊ฐ์ฒด๋ ์ปจํ ์คํธ ๊ฐ์ฒด๋ฅผ ์์๋ฐ์ ํน์ ๋ฆฌ์์ค์ ํด๋์ค ๊ทธ๋ฆฌ๊ณ ์ดํ๋ฆฌ์ผ์ด์ ํ๊ฒฝ์ ๋ํ ์ ๋ณด์ ์ ๊ทผํ ์ ์๊ฒ ํด์ค๋๋ค.
Fragment๋ RecyclerViewAdapter ๋ฑ Context๋ฅผ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ์ง ์๋ ๊ณณ์ด ์์ต๋๋ค.
Fragment๋ ์๋ช ์ฃผ๊ธฐ ์ค onAttach()๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ๋ฉด ์ธ์๋ก context๋ฅผ ๋ฐ์์ค๋ฉฐ, RecyclerViewAdapter๋ onCreateViewHolder ๋ฉ์๋์์ ์ธ์๋ก ๋ฐ์์ค๋ parent(viewGroup)์์ context๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.
์ค์ํ ์ ์ ์ปจํ
์คํธ๋ฅผ ์ ๋ชป ์ฌ์ฉํด์ ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํ๊ฒ ๋๊ณ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ด๊ณผํ๋ฉด Crash(๋น์ ์ ์ข
๋ฃ)๊ฐ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ ์์
์ ๋ฐ๋ผ Activity Context์ Application Context๋ฅผ ์ ํ์ฉํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
Activity Context : ์กํฐ๋นํฐ ๋ด์์ ์ ํจํ ๊ฒฝ์ฐ ์ฌ์ฉ
Application Context : ํ์ฌ ์ปจํ
์คํธ๊ฐ ์ข
๋ฃ๋ ํ์๋ ์ ํ๋ฆฌ์ผ์ด์
์ ์ฒด์์ ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ ์ฌ์ฉ
์์ : ์ดํ๋ฆฌ์ผ์ด์
์์ ์ ์ฒด์์ ์ฌ์ฉ๋๋ ์ฑ๊ธํด ์ค๋ธ์ ํธ์์ Activity Context๋ฅผ ์ฌ์ฉํ๋ฉด ์กํฐ๋นํฐ๊ฐ ์ข
๋ฃ๋ ํ์๋ ํด๋น ์ค๋ธ์ ํธ๊ฐ ์กํฐ๋นํฐ๋ฅผ ๊ณ์ ์ฐธ์กฐํ๊ธฐ ๋๋ฌธ์ ์กํฐ๋นํฐ๊ฐ ๊ฐ๋น์ง ์ปฌ๋ ์
๋์ง์์์ ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์