{"id":442,"date":"2018-07-05T16:08:20","date_gmt":"2018-07-05T07:08:20","guid":{"rendered":"http:\/\/www.charlezz.com\/?p=442"},"modified":"2018-07-15T00:19:05","modified_gmt":"2018-07-14T15:19:05","slug":"fragment%ec%99%80-onsaveinstance-feat-viewpager","status":"publish","type":"post","link":"https:\/\/charlezz.com\/?p=442","title":{"rendered":"Fragment\uc640 onSaveInstance (feat.ViewPager)"},"content":{"rendered":"<p>Fragment\ub294 Runtime\uc5d0 \ub2e8\ub3c5\uc73c\ub85c \uc2e4\ud589\ub420 \uc218 \uc5c6\uc73c\uba70, \ubc18\ub4dc\uc2dc Activity\uac00 \ud544\uc694\ud558\ub2e4.<br \/>\n\ub300\ud45c\uc801\uc778 configuration change\uc778 \uc2a4\ud06c\ub9b0 \ud68c\uc804\uc740 Activity\ub97c \uc7ac\uc0dd\uc131\ud558\uac8c \ub418\uace0, \uc774\ub54c\u00a0\uac1c\ubc1c\uc790\ub294 onSaveInstanceState()\uc640 onRestoreInstanceState() \ucf5c\ubc31\uc744 \uc624\ubc84\ub77c\uc774\ub4dc \ud558\uc5ec, \ubdf0\ub098 \uae30\ud0c0 \uc0c1\ud0dc\ub4f1\uc744 \uc720\uc9c0\ud560 \uc218 \uc788\uac8c \ub41c\ub2e4.<br \/>\n\ubb3c\ub860 \ub9e4\ub2c8\ud398\uc2a4\ud2b8\uc5d0\uc11c \uc561\ud2f0\ube44\ud2f0\uc758 configurationChange\uc635\uc158\uc744 \ucd94\uac00\ud558\uc5ec\uc11c Activity\uac00 \uc7ac\uc0dd\uc131\ub418\uc9c0 \uc54a\uac8c \ud558\ub294 \ubc29\ubc95 \ud558\ub098\uc758 \ubc29\ubc95\uc774 \ub420 \uc218 \uc788\ub2e4.<br \/>\n\uc561\ud2f0\ube44\ud2f0\uac00 \uc7ac\uc0dd\uc131 \ub418\uba74 \uc561\ud2f0\ube44\ud2f0\uc5d0 \uc18d\ud574 \uc788\ub358 \ud504\ub808\uadf8\uba3c\ud2b8\ub294 \uc5b4\ub5bb\uac8c \ub420\uae4c?<\/p>\n<pre class=\"lang:java decode:true \"> override fun onCreate(savedInstanceState: Bundle?) {\r\n        super.onCreate(savedInstanceState)\r\n        setContentView(R.layout.activity_main)\r\n        if(savedInstanceState==null){\r\n            supportFragmentManager.beginTransaction()\r\n                    .replace(R.id.container, MyFragment.newInstance(\"Hello\"))\r\n                    .commitNow()\r\n        }\r\n    }<\/pre>\n<p>&nbsp;<\/p>\n<p>\ubcf4\ud1b5 \ud504\ub808\uadf8\uba3c\ud2b8\ub97c \uad6c\ud604\ud558\uac70\ub098 \uad6c\uae00 \uc0d8\ud50c \uc608\uc81c\ub97c \ud655\uc778\ud574\ubcf4\uba74 \uc774\ub7f0\uc2dd\uc758 \ucf54\ub4dc\ub97c \uc0ac\uc6a9\ud55c\ub2e4.<br \/>\n\uc65c savedInstanceState\uac00 null\uc778\uc9c0 check\ud560\uae4c?<br \/>\nActivity\uac00 \ucd5c\ucd08 \uc0dd\uc131 \ub418\uc5c8\ub2e4\uba74 savedInstanceState\ub294 null\uc774\ub2e4.<br \/>\nActivity\uac00 \uc7ac\uc0dd\uc131\ub418\uc5b4 onCreate()\uac00 \ub2e4\uc2dc \ud638\ucd9c \ub418\uc5c8\ub2e4\uba74 savedInstanceState\ub294 null\uc774 \uc544\ub2c8\ub2e4.<br \/>\n\uadf8\ub7ec\uba74 \uc5ec\uae30\uc11c \uad81\uae08\uc99d\uc774 \ud558\ub098 \uc0dd\uae34\ub2e4.<br \/>\nsavedInstanceState\uc758 null\uccb4\ud06c\ub294 \uc561\ud2f0\ube44\ud2f0\uac00 \uc7ac\uc0dd\uc131 \ub418\uc5c8\ub294\uc9c0 \uccb4\ud06c\ud560 \uc218 \uc788\ub294 \uc218\ub2e8\uc774 \ub418\ub294\uac83\uc774\uace0, \uc7ac\uc0dd\uc131 \ub418\uc5c8\uc744\ub54c\ub294 \ud504\ub808\uadf8\uba3c\ud2b8\ub97c \ud654\uba74\uc5d0 \ubcf4\uc5ec\uc8fc\uc9c0 \uc54a\ub294\uac83\uc778\uac00?<br \/>\n\uc544\ub2c8\ub2e4. \ud504\ub808\uadf8\uba3c\ud2b8\ub294 \uc798 \ub098\uc628\ub2e4. \uc774\uc0c1\ud558\ub2e4. \ubd84\uba85 \uc561\ud2f0\ube44\ud2f0\ub294 \uc7ac\uc0dd\uc131\ub41c\uac83\uc774\uace0, \ud504\ub808\uadf8\uba3c\ud2b8\ub97c \ucd94\uac00\ud55c\uc801\uc774 \uc5c6\ub294\ub370, \ud504\ub808\uadf8\uba3c\ud2b8\uac00 \ud654\uba74\uc5d0 \ub098\uc628\ub2e4.<br \/>\n\uc774\uc720\ub294 \uc774\ub807\ub2e4.<br \/>\n<strong>\uc774\ubbf8 \uc0dd\uc131\ub418\uc5b4 \ucd94\uac00\ub418\uc5c8\ub358 \ud504\ub808\uadf8\uba3c\ud2b8\ub4e4\uc740 configuration change\uc5d0\ub3c4 \ud504\ub808\uadf8\uba3c\ud2b8\ub9e4\ub2c8\uc800\uc5d0\uc758\ud574 \uc0b4\uc544\ub0a8\uc544 \uc790\ub3d9\uc73c\ub85c \uad00\ub9ac\uac00 \ub41c\ub2e4.\u00a0<\/strong><br \/>\n\uadf8\ub7ec\ubbc0\ub85c \uc561\ud2f0\ube44\ud2f0\uac00 \uc7ac\uc0dd\uc131\ub418\uc5c8\uc744\ub54c \ub2e4\uc2dc \ud504\ub808\uadf8\uba3c\ud2b8\ub97c add\ud558\uac70\ub098 replace\ud558\uc9c0 \uc54a\uc544\ub3c4 \ud654\uba74\uc5d0 \ud45c\uc2dc\ub418\uc5c8\ub358 \uac83\uc774\ub2e4.<\/p>\n<p>\uadf8\ub807\ub2e4\uba74 activity\uc5d0 ViewPager\ub97c \uad6c\ud604\ud55c \uacbd\uc6b0\ub294 \uc5b4\ub5bb\uac8c \ub420\uae4c?<br \/>\nviewpager\ub97c \uad6c\ud604\ud558\uae30 \uc704\ud574\uc11c\ub294 (FragmentStatePage)Adapter\uac00 \ud558\ub098 \ud544\uc694\ud558\uace0, \ud398\uc774\uc9c0\ub85c \uc4f8 Fragment\uac00 \ud544\uc694\ud558\ub2e4.<br \/>\nMainActivity.kt<\/p>\n<pre class=\"lang:java decode:true \">override fun onCreate(savedInstanceState: Bundle?) {\r\n    super.onCreate(savedInstanceState)\r\n    Log.e(TAG,\"onCreate\")\r\n    setContentView(R.layout.activity_main)\r\n    pager.adapter = MyAdapter(supportFragmentManager)\r\n}<\/pre>\n<p>activity_main.xml<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;android.support.constraint.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\".MainActivity\"&gt;\r\n    &lt;android.support.v4.view.ViewPager\r\n        android:layout_width=\"0dp\"\r\n        android:id=\"@+id\/pager\"\r\n        app:layout_constraintLeft_toLeftOf=\"parent\"\r\n        app:layout_constraintRight_toRightOf=\"parent\"\r\n        app:layout_constraintTop_toTopOf=\"parent\"\r\n        app:layout_constraintBottom_toBottomOf=\"parent\"\r\n        android:layout_height=\"0dp\"\/&gt;\r\n&lt;\/android.support.constraint.ConstraintLayout&gt;<\/pre>\n<p>MyFragment.kt<\/p>\n<pre class=\"lang:java decode:true\">class MyFragment : Fragment() {\r\n    companion object {\r\n        fun newInstance(s: String): MyFragment {\r\n            return MyFragment().apply {\r\n                arguments = Bundle().apply { putString(\"data\", s) }\r\n            }\r\n        }\r\n    }\r\n    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {\r\n        val view = inflater.inflate(R.layout.fragment_main, container, false)\r\n        view.text.text = arguments?.getString(\"data\",\"default\")\r\n        return view\r\n    }\r\n}<\/pre>\n<p>fragment_main.kt<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;android.support.constraint.ConstraintLayout\r\n    xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"&gt;\r\n    &lt;TextView\r\n        android:layout_width=\"match_parent\"\r\n        android:id=\"@+id\/text\"\r\n        android:gravity=\"center\"\r\n        android:autoSizeTextType=\"uniform\"\r\n        android:layout_height=\"match_parent\" \/&gt;\r\n&lt;\/android.support.constraint.ConstraintLayout&gt;<\/pre>\n<p>MyAdapter.kt<\/p>\n<pre class=\"lang:java decode:true  \">class MyAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {\r\n\r\n    override fun getItem(index: Int): MyFragment = MyFragment.newInstance(index.toString())\r\n    override fun getCount(): Int = 5\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>NOTE : \ud14c\uc2a4\ud2b8\ub97c \uc704\ud574 \ubc18\ub4dc\uc2dc \uac1c\ubc1c\uc790 \uc635\uc158\uc5d0\uc11c &#8216;\ud65c\ub3d9 \ubcf4\uad00 \uc548 \ud568&#8217; \uc635\uc158\uc744 ON\uc73c\ub85c \ud558\uc790. configuration \ubcc0\ud654\uc2dc activity \ub610\ub294 fragment \uc758 \uc778\uc2a4\ud134\uc2a4\ub97c \uac15\uc81c\ub85c \uc7ac\uc0dd\uc131 \ud55c\ub2e4.<\/strong><\/p>\n<p>\ud398\uc774\uc9c0 \ud654\uba74\uc5d0 \ubcf4\uc778\ub2e4\uba74 \uc801\ub2f9\ud55c \uc778\ub371\uc2a4\ub85c \uc62e\uae34\ub4a4 \ud654\uba74 \ud68c\uc804(Screen Rotation)\uc744 \ud574\ubcf4\uac70\ub098, \ub2e4\ub978 \uc571\uc744 \uc654\ub2e4 \uac14\ub2e4 \uc774\ub3d9\uc744 \ud558\uba74\uc11c \ud398\uc774\uc9c0\uac00 \uc720\uc9c0\ub418\ub294\uc9c0 \ud655\uc778\ud574\ubcf4\uc790.<br \/>\nViewPager\uc640 FragmentStatePagerAdapter\ub3c4 \ub611\uac19\ub2e4. \uc6b0\uc120 \uc5b4\ub311\ud130\ub97c \ud1b5\ud574 \ud504\ub798\uadf8\uba3c\ud2b8\ub97c \ub9e4\ub2c8\uc800\uc5d0 \ucd94\uac00\ud558\uba74 \ud654\uba74 \ud68c\uc804 \ub4a4\uc5d0\ub3c4 getItem(int position)\uc740 \ud638\ucd9c\ub418\uc9c0 \uc54a\ub294\ub2e4. \ub300\uc2e0\uc5d0 \ud504\ub808\uadf8\uba3c\ud2b8\ub9e4\ub2c8\uc800 \ub0b4\ubd80\uc5d0\uc11c \ud504\ub798\uadf8\uba3c\ud2b8\uc758 \uae30\ubcf8\uc0dd\uc131\uc790\ub97c \ud1b5\ud574 \ud504\ub798\uadf8\uba3c\ud2b8\ub97c \uc7ac\uc0dd\uc131\ud558\uba70 \uc774\ub97c \ucc38\uc870\ud558\uac8c \ub41c\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fragment\ub294 Runtime\uc5d0 \ub2e8\ub3c5\uc73c\ub85c \uc2e4\ud589\ub420 \uc218 \uc5c6\uc73c\uba70, \ubc18\ub4dc\uc2dc Activity\uac00 \ud544\uc694\ud558\ub2e4. \ub300\ud45c\uc801\uc778 configuration change\uc778 \uc2a4\ud06c\ub9b0 \ud68c\uc804\uc740 Activity\ub97c \uc7ac\uc0dd\uc131\ud558\uac8c \ub418\uace0, \uc774\ub54c\u00a0\uac1c\ubc1c\uc790\ub294 onSaveInstanceState()\uc640 onRestoreInstanceState() \ucf5c\ubc31\uc744 \uc624\ubc84\ub77c\uc774\ub4dc \ud558\uc5ec, \ubdf0\ub098 \uae30\ud0c0 \uc0c1\ud0dc\ub4f1\uc744 \uc720\uc9c0\ud560 \uc218 \uc788\uac8c \ub41c\ub2e4. \ubb3c\ub860 \ub9e4\ub2c8\ud398\uc2a4\ud2b8\uc5d0\uc11c \uc561\ud2f0\ube44\ud2f0\uc758 configurationChange\uc635\uc158\uc744 \ucd94\uac00\ud558\uc5ec\uc11c Activity\uac00 \uc7ac\uc0dd\uc131\ub418\uc9c0 \uc54a\uac8c \ud558\ub294 \ubc29\ubc95 \ud558\ub098\uc758 \ubc29\ubc95\uc774 \ub420 \uc218 \uc788\ub2e4. \uc561\ud2f0\ube44\ud2f0\uac00 \uc7ac\uc0dd\uc131 \ub418\uba74 \uc561\ud2f0\ube44\ud2f0\uc5d0 \uc18d\ud574 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[16],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/442"}],"collection":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=442"}],"version-history":[{"count":2,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/442\/revisions"}],"predecessor-version":[{"id":581,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/442\/revisions\/581"}],"wp:attachment":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}