Lập trình android chapter03 life cycle

48 252 0
Lập trình android chapter03 life cycle

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Part Android Application’s Life Cycle Notes are based on: Unlocking Android by Frank Ableson, Charlie Collins, and Robi Sen ISBN 978-1-933988-67-2 Manning Publications, 2009 Android Developers http://developer.android.com/index.html Android – Application's Life Cycle Outline • Android Applications • • • Activity, Service, Broadcast receiver, Content provider process Application’s Life Cycle • • • • • • Activity Stack Life Cycle States Life Cycle Events Application’s Lifetime: Visible Lifetime Foreground Lifetime Life Cycle Methods: onCreate(), onStart(), onDestroy() Example: LifeCycleDemo Android – Application's Life Cycle Android Applications Một ứng dụng (application) bao gồm vài component định nghĩa manifest file ứng dụng, gồm loại: Activity Service BroadcastReceiver ContentProvider Ứng dụng Android so với Java application: • • • Cũng viết class để thực công việc Không dùng hàm main khởi tạo object thuộc class gọi phương thức Tùy theo loại object (Activity, Service ), Android gọi constructor quản lý vòng đời object 3 Android – Application's Life Cycle Android Applications Activity Một activity thường trình diễn giao diện người dùng đơn hình ảnh mà từ thực số hành động (action) Tuy activity cộng tác với để hợp thành giao diện người dùng thống nhất, activity có tính độc lập với activity khác Thông thường, activity đánh dấu làm activity – trình diễn cho người dùng ứng dụng bật lên Việc gọi activity từ bên activity khác thực qua việc activity hành gọi activity qua chế intent (startActivity(Intent)) 4 Android – Application's Life Cycle Android Applications Service Một service giao diện người dùng hình ảnh, thay vào đó, chạy ẩn background khoảng thời gian không xác định Có thể kết nối (bind to) với service chạy (hoặc chạy service chưa chạy) Trong kết nối, ta liên lạc với service qua giao diện mà cung cấp 5 Android – Application's Life Cycle Android Applications Broadcast receiver Một broadcast receiver component không làm việc nhận phản ứng với broadcast announcement Nhiều broadcast bắt nguồn từ mã hệ thống (ví dụ “you got mail“), ứng dụng phát broadcast Broadcast receiver không hiển thị giao diện người dùng Tuy nhiên, chúng bật activity để đáp ứng thông tin mà chúng nhận được, - giống cách làm service – chúng dùng notification manager để thông báo cho người dùng 6 Android – Application's Life Cycle Android Applications Content provider Một content provider cung cấp tập liệu ứng dụng cho ứng dụng khác Dữ liệu thường lưu hệ thống file, CSDL SQLite Mỗi content provider cài đặt tập chuẩn phương thức cho phép ứng dụng khác lấy lưu trữ liệu thuộc loại mà kiểm soát Tuy nhiên, ứng dụng không gọi trực tiếp phương thức Thay vào đó, chúng dùng đối tượng content resolver gọi phương thức đối tượng Một content resolver nói chuyện với content provider bất kì; cộng tác với provider để quản lý trình liên lạc xuyên tiến trình (interprocess communication) phát sinh 7 Android – Application's Life Cycle Android Applications Mỗi ứng dụng Android chạy tiến trình riêng (cùng với thực thể máy ảo Dalvik riêng nó) Mỗi có request cần xử lý component cụ thể đó, • • • Android đảm bảo tiến trình ứng dụng component chạy, khởi động cần, đảm bảo có sẵn thực thể (instance) component đó, cần tạo thực thể 8 Android – Application's Life Cycle Application’s Life Cycle Một tiến trình Linux dành cho ứng dụng Android tạo cho ứng dụng có số phần code ứng dụng cần chạy Tiến trình tồn không cần đến nữa, HOẶC  hệ thống cần thu hồi nhớ ứng dụng khác dùng   9 Android – Application's Life Cycle Application’s Life Cycle   Một tính chất đặc biệt Android tiến trình không trực tiếp kiểm soát vòng đời Thay vào đó, số phận hệ thống định dựa thông tin Các phần ứng dụng chạy, Tầm quan trọng chúng người dùng Hệ thống nhớ 10 10 Android – Application's Life Cycle Example: Life Cycle Code: Life Cycle Demo Part @Override protected void onPause() { super.onPause(); saveDataFromCurrentState(); Toast.makeText(this, "onPause", 1).show(); } @Override protected void onRestart() { super.onRestart(); Toast.makeText(this, "onRestart", 1).show(); } @Override protected void onResume() { super.onResume(); Toast.makeText(this, "onResume", 1).show(); } 34 34 Android – Application's Life Cycle Example: Life Cycle Code: Life Cycle Demo Part @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); updateFromSavedState(); Toast.makeText(this, "onStart", 1).show(); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Toast.makeText(this, "onDestroy", 1).show(); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Toast.makeText(this, "onStop", 1).show(); } 35 35 Android – Application's Life Cycle Example: Life Cycle Code: Life Cycle Demo Part protected void saveDataFromCurrentState() { SharedPreferences myPrefs = getSharedPreferences(MYPREFSID, actMode); SharedPreferences.Editor myEditor = myPrefs.edit(); myEditor.putString("myBkColor", txtColorSelect.getText().toString()); myEditor.commit(); } // saveDataFromCurrentState protected void updateFromSavedState() { SharedPreferences myPrefs = getSharedPreferences(MYPREFSID, actMode); if ((myPrefs != null) && (myPrefs.contains("myBkColor"))) { String theChosenColor = myPrefs.getString("myBkColor",""); txtColorSelect.setText(theChosenColor); changeBackgroundColor(theChosenColor); } } // updateFromSavedState protected void clearMyPreferences() { SharedPreferences myPrefs = getSharedPreferences(MYPREFSID, actMode); SharedPreferences.Editor myEditor = myPrefs.edit(); myEditor.clear(); myEditor.commit(); } // clearMyPreferences 36 Android – Application's Life Cycle Example: Life Cycle Code: Life Cycle Demo Part private static String MYPREFSID; //used in part private static int actMode; //used in part private void changeBackgroundColor (String theChosenColor){ // change background color if (theChosenColor.contains("red")) myScreen.setBackgroundColor(0xffff0000); else if (theChosenColor.contains("green")) myScreen.setBackgroundColor(0xff00ff00); else if (theChosenColor.contains("blue")) myScreen.setBackgroundColor(0xff0000ff); else { //reseting user preferences clearMyPreferences(); myScreen.setBackgroundColor(0xff000000); } } 37 37 Android – Application's Life Cycle Example: Life Cycle Code: Life Cycle Demo Part /* protected void onRestoreInstanceState(Bundle savedInstanceState) This method is called after onStart() when the activity is being re-initialized from a previously saved state The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle) Phương thức gọi sau onStart() activity khơở i tạo lại từ trạng thái lưu lại trước Cài đặt mặc định cuở a phương thức khôi phục trạng thái view lưu bơở i onSaveInstanceState(Bundle) */ @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Toast.makeText(getBaseContext(), "onRestoreInstanceState BUNDLING", Toast.LENGTH_LONG).show(); } 38 38 Android – Application's Life Cycle Example: Life Cycle Code: Life Cycle Demo Part /* protected void onSaveInstanceState(Bundle outState) Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both) This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via: onCreate(Bundle) or onRestoreInstanceState(Bundle) Phương thức gọi trước activity có thểởbị kill cho quay lại có thểởphục hôồ i trạng thái cuở a Ví dụ, activity A bị hệ thôố ng kill đểởlấố y tài nguyển, A có hội lưu trạng thái hành cuở a giao diện người dùng phương thức này, đểở người dùng quay lại activity A, trạng thái cuở a giao diện người dùng có thểởđược khôi phục qua phương thức onCreate(Bundle) onRestoreInstanceState(Bundle) (Đôố i tượng Bundle mà phương thức xấy dựng truyểồ n cho caởhai phương thức khôi phục đó) */ 39 39 Android – Application's Life Cycle Example: Life Cycle Code: Life Cycle Demo Part 10 @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Toast.makeText(getBaseContext(), "onSaveInstanceState .BUNDLING", Toast.LENGTH_LONG).show(); } // onSaveInstanceState }//LifeCycleDemo 40 40 Android – Application's Life Cycle Example: Life Cycle onCreate… onStart… onResume… 41 41 Android – Application's Life Cycle Example: Life Cycle onPause… onStop… onDestroy… After pressing “Back Arrow” 42 42 Application’s Life Cycle Questions ? 43 Application’s Life Cycle Appendix Lưu thông tin trạng thái khác @Override public void onCreate(Bundle savedInstanceState) { somevalue = savedInstanceState.getString(SOME_KEY); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString(SOME_KEY, "blah blah blah"); } 44 Android – Application's Life Cycle Homework Your turn! Teaching notes EXPERIMENT 1.Viết ứng dụng Android (“PuraVida”) để thử nghiệm vòng đời khác ứng dụng 2.Layout main.xml cần có Button (text: “Finish”, id: btnFinish) EditText container (txt: “” , id: txtMsg) 3.Dùng phương thức onCreate để nối button textbox với chương trình Thêm dòng mã sau: Toast.makeText(this, "onCreate", 1).show(); Phương thức click có lệnh: finish(); dùng để kết thúc ứng dụng Thêm lệnh Toast (tương tự trên) cho event event lại Để đơn giản, dùng menu Source > Override/Implement Methods… Eclipse Tại cửa sổ option, đánh dấu event sau: onStart, onResume, onPause, onStop, onDestroy, onRestart (để ý xem có phương thức onEvent… đó!!!) 6.Lưu chương trình 45 Android – Application's Life Cycle Homework Your turn! EXPERIMENT (cont.) Teaching notes 7.Dịch chạy ứng dụng 8.Ghi lại chuỗi message hiển thị lệnh Toast 9.Nhấn nút FINISH Quan sát chuỗi trạng thái 10.Chạy lại ứng dụng 11.Nhấn nút HOME emulator Chuyện xảy ra? 12.Click vào launch pad, tìm icon quay lại ứng dụng “PuraVida” Chuỗi message hiển thị? 13.Click phím CALL emulator Ứng dụng trạng thái paused hay stopped? 14.Click phím BACK để quay lại ứng dụng 15.Long-tap vào nút HANG-UP Chuyện xảy ra? 46 Android – Application's Life Cycle Homework Your turn! EXPERIMENT Teaching notes 16.Chạy emulator thứ hai Thực voice-call tới emulator thứ hiển thị ứng dụng bạn Chuyện xảy ra? (yêu cầu đồng thời gian thực) Gửi text-message tới emulator thứ (yêu cầu không đồng - asynchronous attention request) 17.Viết câu vào EditText box (“these are the best moments of my life….”) 18.Chạy lại ứng dụng Chuyện xảy dòng text? 47 Android – Application's Life Cycle Homework Your turn! Teaching notes EXPERIMENT Đảm bảo tính bền vững liệu (data persistency) 19 Thêm vào phương thức onPause đoạn sau SharedPreferences myFile1 = getSharedPreferences("myFile1", Activity.MODE_PRIVATE); SharedPreferences.Editor myEditor = myFile1.edit(); String temp = txtMsg.getText().toString(); myEditor.putString("mydata", temp); myEditor.commit(); 20 Thêm vào phương thức onResume đoạn sau SharedPreferences myFile = getSharedPreferences("myFile1", Activity.MODE_PRIVATE); if ( (myFile != null) && (myFile.contains("mydata")) ) { String temp = myFile.getString("mydata", "***"); txtMsg.setText(temp); } 21 Giờ chuyện xảy với liệu nhập vào text box? 48 ... provider để quản lý trình liên lạc xuyên tiến trình (interprocess communication) phát sinh 7 Android – Application's Life Cycle Android Applications Mỗi ứng dụng Android chạy tiến trình riêng (cùng... activity thường trình diễn giao diện người dùng đơn hình ảnh mà từ thực số hành động (action) Tuy activity cộng tác với để hợp thành giao diện người dùng thống nhất, activity có tính độc lập với activity... tiến trình ứng dụng component chạy, khởi động cần, đảm bảo có sẵn thực thể (instance) component đó, cần tạo thực thể 8 Android – Application's Life Cycle Application’s Life Cycle Một tiến trình

Ngày đăng: 07/09/2017, 23:15

Từ khóa liên quan

Mục lục

  • Slide 1

  • Slide 2

  • Slide 3

  • Slide 4

  • Slide 5

  • Slide 6

  • Slide 7

  • Slide 8

  • Slide 9

  • Slide 10

  • Slide 11

  • Slide 12

  • Slide 13

  • Slide 14

  • Slide 15

  • Slide 16

  • Slide 17

  • Slide 18

  • Slide 19

  • Slide 20

Tài liệu cùng người dùng

Tài liệu liên quan