Print
Details: Category: บทความ Flutter | Published: 20 December 2019 | Hits: 2801

LiftCycleState on Flutter ดูภาพ นี่ครับ

Screen Shot 2562 12 03 at 043902

ดูภาพ นี่นะครับ เมื่อ State สร้างขึ้น และ ถูก Paused จากการ กด Home หรือ ปิดหน้าจอ และ กลับมาเรียกใช้ใหม่ โดย Resume ครับ

Screen Shot 2562 12 03 at 045220

มาที่ Home Start ครับ

 Screen Shot 2562 12 03 at 0456

ให้ overide เมธอด didChangeAppLifecycleState ขึ้นมา

Screen Shot 2562 12 03 at 045757

และ override initState

Screen Shot 2562 12 03 at 045822

Override dispose

Screen Shot 2562 12 03 at 055843

โดยในเมธอด initState, dispose มาสเตอร์ จะ print ข้อความตามนี่ ว่าใครทำงาน ให้บอกออกมาที่ Console อย่าลืม WidgetBinding.instance.addObserver ด้วย

Screen Shot 2562 12 03 at 062716

เพื่อให้ didChangeAppLifeccleState แสดง State ต่างๆ ด้วย

 Screen Shot 2562 12 03 at 062816

ทีนี่มาสเตอร์ ลองรัน ดูนะครับ

Screen Shot 2562 12 03 at 062824

ดูที่ Console จะเห็นข้อความ ####### iniStare Work ########

จำโค้ดตรงนี่ได้ไหม ?

Screen Shot 2562 12 03 at 055843

นั้นหมายความว่า initStare จะทำงานเมื่อ แอพเรารัน ครั้งแรก ก่อน

Screen Shot 2562 12 03 at 063140

จากนั้นมาสเตอร์ กดปุ่ม สีเหลี่ยม

Screen Shot 2562 12 03 at 063154

 

ตอนนี่คุณ inactive และ paused อยู่นะ

จำตรงนี่ได้นะ

Screen Shot 2562 12 03 at 062716 นี่ไง

Screen Shot 2562 12 03 at 063211

เลือกกลับมา State Home ใหม่

Screen Shot 2562 12 03 at 063221 

จะเห็น inactive และ resume ครับ

Screen Shot 2562 12 03 at 070003

ที่นี่ มาสเตอร์ กดปุ่ม Home เลย

Screen Shot 2562 12 03 at 070011

เห็นไหม ? จะมา inactive และ paused

Screen Shot 2562 12 03 at 070031

กดปุ่ม สี่เหลี่ยม 

Screen Shot 2562 12 03 at 070041

และ เปิด แอพมาใหม่

Screen Shot 2562 12 03 at 070045

ใน Console จะแจ้งว่า คุณ resumed แบบนี้ครับ

ค่อยๆ ทำความเข้าใจไปนะครับ แต่ถ้าไม่ได้อย่างไร ? มาเรียน คอร์ส Flutter ตัวต่อตัว กับมาสเตอร์ได้ที่ 

 

#flutter, #สอนflutter, #โค้ดflutter, #dart, #สอนภาษาdart, #สอนเขียนแอพแอนดรอยด์, #สอนเขียนแอพiOS, #flutterสอน

 

ดูเพิ่มเติมที่

https://www.androidthai.in.th/private-project-android-flutter-course.html ครับ