Activity.ppt
Upcoming SlideShare
Loading in...5
×
 

Activity.ppt

on

  • 1,340 views

 

Statistics

Views

Total Views
1,340
Views on SlideShare
1,339
Embed Views
1

Actions

Likes
0
Downloads
20
Comments
0

1 Embed 1

http://www.slideshare.net 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Activity.ppt Activity.ppt Presentation Transcript

    • Android 中的 Activity 物件
    • Activity 的繼承關係
      • 繼承樹
      java.lang.Object android.content.Context android.app.ApplicationContext android.app.Activity
    • Activity 物件
      • 代表一個活動,通常伴隨著 View 界面
      • 例如
        • 主程式
        • 編輯功能
    • Activity 的生命週期
    • Activity 的範例 public class MyActivity extends Activity { ... static final int PICK_CONTACT_REQUEST = 0; protected boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { // When the user center presses, let them pick a contact. startSubActivity( new Intent(Intent.PICK_ACTION, "content://contacts"), PICK_CONTACT_REQUEST); return true; } return false; } protected void onActivityResult (int requestCode, int resultCode, String data, Bundle extras) { if (requestCode == PICK_CONTACT_REQUEST) { if (resultCode == RESULT_OK) { // A contact was picked. Here we will just display it // to the user. startActivity(new Intent(Intent.VIEW_ACTION, data)); } } } }
    • 重要的 Activiey 成員函數
      • setContentView(View view, LayoutParams params)
      • setContentView(View view)
      • setContentView(int layoutResID)
        • Ex : setContentView(R.layout. save_restore_state);
      • findViewById(R.id. msg)
      • getSharedPreferences ( String name, int mode)
      • getPreferences (int mode)
      • setResult(int resultCode, String data)
      • setResult(int resultCode, String data, Bundle extras)
      • setResult(int resultCode)
      • startActivity ( Intent intent)
      • startSubActivity ( Intent intent, int requestCode)
    • Persistence
      • Edit in place 儲存法則
        • any edits a user makes are effectively made immediately without requiring an additional confirmation step
          • When creating a new document, the backing database entry or file for it is created immediately. For example, if the user chooses to write a new e-mail, a new entry for that e-mail is created as soon as they start entering data, so that if they go to any other activity after that point this e-mail will now appear in the list of drafts.
          • When an activity's onPause() method is called, it should commit to the backing content provider or file any changes the user has made. This ensures that those changes will be seen by any other activity that is about to run. You will probably want to commit your data even more aggressively at key times during your activity's lifecycle: for example before starting a new activity, before finishing your own activity, when the user switches between input fields, etc.
      • 原因
        • This model is designed to prevent data loss when a user is navigating between activities, and allows the system to safely kill an activity (because system resources are needed somewhere else) at any time after it has been paused.
        • Note this implies that the user pressing BACK from your activity does not mean "cancel" -- it means to leave the activity with its current contents saved away. Cancelling edits in an activity must be provided through some other mechanism, such as an explicit "revert" or "undo" option.
    • Persistence 的範例 public class CalendarActivity extends Activity { ... static final int DAY_VIEW_MODE = 0; static final int WEEK_VIEW_MODE = 1; private SharedPreferences mPrefs; private int mCurViewMode; protected void onCreate(Bundle icicle) { super.onCreate(icicle); SharedPreferences mPrefs = getSharedPreferences(); mCurViewMode = mPrefs.getInt("view_mode" DAY_VIEW_MODE); } protected void onPause() { super.onPause(); SharedPreferences.Editor ed = mPrefs.edit(); ed.putInt("view_mode", mCurViewMode); ed.commit(); } }
    • Persistence 的相關函數
      • SharedPreferences getPreferences(int mode)
        • mode Operating mode. Use 0 for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions.
        • This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.
      • android.content.SharedPreferences
        • Interface for accessing and modifying preference data returned by getSharedPreferences(String, int) .
      • android.content.Context
    • Process Life Cycle
      • The foreground activity (the activity at the top of the screen that the user is currently interacting with) is considered the most important. Its process will only be killed as a last resort, if it uses more memory than is available on the device. Generally at this point the device has reached a memory paging state, so this is required in order to keep the user interface responsive.
      • A visible activity (an activity that is visible to the user but not in the foreground, such as one sitting behind a foreground dialog) is considered extremely important and will not be killed unless that is required to keep the foreground activity running.
      • A background activity (an activity that is not visible to the user and has been paused) is no longer critical, so the system may safely kill its process to reclaim memory for other foreground or visible processes. If its process needs to be killed, when the user navigates back to the activity (making it visible on the screen again), its onCreate(Bundle) method will be called with the icicle it had previously supplied in onFreeze(Bundle) so that it can restart itself in the same state as the user last left it.
      • An empty process is one hosting no activities or other application components (such as Service or IntentReceiver classes). These are killed very quickly by the system as memory becomes low. For this reason, any background operation you do outside of an activity must be executed in the context of an activity IntentReceiver or Service to ensure that the system knows it needs to keep your process around