Start a Fragment for results ใน Android?

“Google ได้เพิ่มความสามารถใหม่ให้ FragmentManager ซึ่งทำให้ FragmentManager นั้นเป็นเหมือนตัวกลางเก็บ Fragment Results”
💁♂️ Source code ใน project นี้ทั้งหมดสามารถดูได้ที่ https://github.com/BenBoonya/fragment-result
ในการทำงานกับ Android Development ทุกคนน่าจะเคยใช้และคุ้นเคยกับ startActivityForResult กันดี โดยเราเรียกใช้ function นี้ใน Activity หรือ Fragment เพื่อเริ่ม Activity ใหม่และเอา Result กลับมาจาก Activity นั้น แต่ถ้า Screen ต่างๆใน Application ของเราสร้างด้วย Fragment เพียงอย่างเดียวล่ะ? เช่นนั้นแน่นอนว่าเราก็ไม่สามารถใช้ startActivityForResult เพื่อส่งข้อมูลระหว่าง Fragment ได้
จนถึงเมื่อเร็วๆนี้ ถ้าเราต้องการสื่อสารระหว่าง Fragment ด้วยกัน ตัว Framework ยังไม่มีอะไรเหมือน startActivityForResult เพื่อทำหน้าที่นั้น ดังนั้นก่อนหน้านี้ผู้เขียนจึงสร้าง ViewModel ขึ้นมาและ share ViewModel นั้นระหว่าง Fragment เพื่อใช้เป็นตัวกลางในการสื่อสาร
แต่ตอนนี้ Google ได้เพิ่มความสามารถให้ Fragment และเรามีสองวิธีที่จะส่งข้อมูลระหว่าง วิธีแรกใช้ FragmentManager และวิธีที่สองใช้ Navigation Component Library
มาดูกันว่าเราจะใช้ความสามารถใหม่นี้กันอย่างไร? เพื่อให้เห็นภาพชัดเจนยิ่งขึ้นขอยกตัวอย่าง Note Application ง่ายๆ ที่ประกอบไปด้วย Fragment 2 ตัว Fragment แรกแสดงรายการ note และ Fragment ที่สองใช้สำหรับเพิ่ม note เข้าไปในรายการ
มาเริ่มจากการใส่ dependencies ที่สำคัญกันก่อนเลย feature ของ Fragment ที่กล่าวถึงนี้ปรากฎขึ้นครั้งแรกใน lib version 1.3.0 ช่วง alpha. ซึ่งตอนนี้ก็เป็น version 1.3.0-rc01 แล้ว
Navigation graph ของ project จะหน้าตาประมานนี้
Source code แบบใช้ Fragment Manager ดูที่ branch นี้ https://github.com/BenBoonya/fragment-result/tree/using-fragment-manager
ความสามารถใหม่ใน FragmentManager ทำให้ FragmentManager นั้นเป็นเหมือนตัวกลางเก็บ Fragment Results ได้ถึงแม้ว่า function จะไม่ได้ถูกตั้งชื่อว่า startFrgamentForResult แต่การทำงานนั้นก็ใกล้เคียงกับ startActivityForResult เลย ด้วยโค้ดด้านล่างนี้เราสามารถส่งข้อมูลไปมาระหว่าง Fragment ได้ง่ายดาย
สำหรับ NoteListFragment เพื่อที่จะเอา Result จาก AddNoteFragment เราต้อง set result listener ไว้ ทีนี้มาลองดูโค้ดใน NoteListFragment. กัน
อย่างที่เห็นว่าจะมี extension function ชื่อ setFragmentResultListener และเราจะได้ result กลับมาในรูปแบบของ bundle ลองไปดูต่อที่ AddNoteFragment เราต้อง set result ใน fragment นี้และเราสามารถทำได้ง่ายๆตามวิธีด้านล่าง
แต่สิ่งหนึ่งที่ควรคำนึงถึงคือ เราต้อง set result บน FragmentManager เดียวกันโดยใช้ requestKey เหมือนกัน ถ้าลองมองละเอียดขึ้นมาใน extension ของ. setFragmentResult และ setFragmentResultListener จะเห็นว่า ทั้งสอง function เรียกบน parentFragmentManager ดังโค้ดด้านล่างนี้
อีกสิ่งนึงที่ควรกล่าวถึงคือ ถ้าเราเรียก setFragmentResult ใน AddNoteFragment มากกว่า 1 ครั้ง ระบบจะส่ง result ตัวล่าสุดไปที่ NoteListFragment ก่อนที่ AdddNoteFragment จะถูก popped off จาก back stack
ถ้า AddNoteFragment เป็น child fragment ของ NoteListFragment เราก็แค่ต้อง set listener และ result ไปที่. childFragmentManager แทน parentFragmentManager
Source code branch ที่ใช้ navigation component https://github.com/BenBoonya/fragment-result/tree/using-navigation-component
การที่ส่งค่ากลับไปที่ destination ก่อนหน้า Navigation Component ใช้ SavedStateHandle ซึ่งสามารถเก็บข้อมูลแบบ key – value map ที่จะไม่หายไปเวลาเกิด Configuration change ขึ้น ทีนี้มาดูตัวอย่างโค้ดกัน
NoteListFragment
saveStateHandle จะให้ค่าออกมาเป็น LiveData และเราสามารถ observe ที่ LiveData เพื่อเอา value ได้
นี่คือวิธีการ set value ลงไปที่ savedStateHandle สำหรับ previous back stack fragment ซึ่งก็หมายถึง NoteListFragment นั่นเอง
ในความเห็นของผู้เขียนคิดว่าใน Single Activity Architecture ที่ Screen ส่วนใหญ่นั้นคือ Fragment feature ใหม่ที่เพิ่มเข้ามาในตระกูล Fragment นี้ทำให้การทำงานกับ Fragment ง่ายขึ้นมากทีเดียว
บทความนี้เรามาแนะนำโน๊ตบุ๊คทํางาน หรือ เขียนโปรแกรมกัน ในปี 2023 นี้เรียกได้ว่าต้องใช้โน๊ตบุ๊คทำงานแทบทุกอย่าง เรียกว่าแทบทุกอาชีพในสายงาน Office ในโลกยุคปัจจุบันจะขาดอุปกรณ์อย่างโน๊ตบุ๊คไปไม่ได้เลย หากจะบอกว่าสำคัญยิ่งกว่า...
Read Moreเมาส์ไร้สาย ยี่ห้อไหนดี? 5 รายการ เมาส์ถนอมข้อมือ เวลาจะเลือกซื้อเมาส์ หลายๆคนคงมีคำถามว่า เราควรจะใช้ เมาส์ไร้สาย ยี่ห้อไหนดี...
Read Moreหากคุณกำลังมองหา โน๊ตบุ๊ค 2023 ราคาไม่เกิน 15000 เล่นเกม หรือ ทำงานนั้นคำถามที่ต้องคิดให้ดีเลยคือ อะไรที่เราต้องการและสำคัญที่สุด และ...
Read Moreบทความนี้เรามาแนะนำโน๊ตบุ๊คทํางาน หรือ เขียนโปรแกรมกัน ในปี 2022 นี้เรียกได้ว่าโน๊ตบุ๊คทำงานได้แทบทุกอย่าง เรียกว่าสายงานในโลกยุคปัจจุบันจะขาดอุปกรณ์อย่างโน๊ตบุ๊คไปไม่ได้เลย เรียกได้ว่าสำคัญยิ่งกว่า กระดาษ ปากกา...
Read MoreCookie | Duration | Description |
---|---|---|
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |