中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

GoogleDoc - 溫故而知新Activity生命周期方法

發布時間:2020-06-09 01:51:37 來源:網絡 閱讀:319 作者:屠夫章哥 欄目:移動開發

3.創建Activity一般人所不知道的地方

  1)Activity里的各個生命周期的方法一般執行什么代碼   

  》》onCreate() method shows some code that performs some fundamental setup for the activity, such as declaring the user interface (defined in an XML layout file), defining member variables, and configuring some of the UI.(填充View,定義變量。。。)

   在onCreate()方法里,對于高版本的API,應判斷一下系統的的版本再決定是否執行。

// Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // For the main activity, make sure the app icon in the action bar
        // does not behave as a button
        ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(false);
    }

   Caution: Using the SDK_INT to prevent older system's from executing new APIs works in this way    on Android 2.0 (API level 5) and higher only. Older versions will encounter a runtime exception.

 

 》》onPause()一般與onStop一起執行,應該注意它的特殊情況。

 As long as the activity is still partially visible but currently not the activity in focus, it       remains paused.(Activity可見,但是沒有獲得焦點就會進行pause狀態)

   As your activity enters the paused state, the system calls theonPause() method on your Activity, which allows you to stop ongoing actions that should not continue while paused (such as a video) or persist any information that should be permanently saved in case the user continues to leave your app. If the user returns to your activity from the paused state, the system resumes it and calls the onResume() method. (在onPause方法里可以停止一些操作,保留信息當用戶離開app的時候,當走出paused狀態時,Activity會執行onResume方法)

 

   You should usually use the onPause() callback to:


    • Stop animations or other ongoing actions that could consume CPU.(停止動畫)

    • Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).

    • Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.(釋放網絡資源)

 

Generally, you should not use onPause() to store user changes (such as personal information entered into a form) to permanent storage. The only time you should persist user changes to permanent storage withinonPause() is when you're certain users expect the changes to be auto-saved (such as when drafting an email). However, you should avoid performing CPU-intensive work during onPause(), such as writing to a database, because it can slow the visible transition to the next activity (you should instead perform heavy-load shutdown operations during onStop()).

You should keep the amount of operations done in the onPause() method relatively simple in order to allow for a speedy transition to the user's next destination if your activity is actually being stopped.

上面的文字中著重強調了,如果想要快速的跳轉到下一個Activity,不要將一些重量級的操作放在onPause里,而應該放在onStop方法中。由下圖也可以領會這個意思:

GoogleDoc - 溫故而知新Activity生命周期方法

Note: When your activity is paused, the Activity instance is kept resident in memory and is recalled when the activity resumes. You don’t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state.

一般初始化相機的方法寫在onResume()方法中,release camera的邏輯寫在onPause中。


 》》onStop() 一般用來釋放資源,防止應用線程被殺死。(按下Home鍵)

   When your activity receives a call to the onStop() method, it's no longer visible and should release almost all resources that aren't needed while the user is not using it. Once your activity is stopped, the system might destroy the instance if it needs to recover system memory. In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.

   When your activity is stopped, the Activity object is kept resident in memory and is recalled when the activity resumes. You don’t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state. The system also keeps track of the current state for each View in the layout, so if the user entered text into an EditText widget, that content is retained so you don't need to save and restore it.(Activity stop之前,對象仍然會保存在內存當中,沒有必要重新初始化組件了)

 Note: Even if the system destroys your activity while it's stopped, it still retains the state of the View objects (such as text in an EditText) in a Bundle (a blob of key-value pairs) and restores them if the user navigates back to the same instance of the activity (the next lesson talks more about using a Bundle to save other state data in case your activity is destroyed and recreated).(當Activity stop的時候,即使Activity被銷毀了,仍然可以保存View的狀態,當用戶開啟新的Activity實例時會恢復它們) 

  》》onRestart() 當Activity從stop狀態返回到前臺時執行這個方法 

  When your activity comes back to the foreground from the stopped state, it receives a call to onRestart().The system also calls the onStart() method, which happens every time your activity becomes visible (whether being restarted or created for the first time) 

  Google重點強調了onRestart與onStart的區別,強烈要求onStart()與onStop配對使用,因為onRestart只有在從stop狀態返回的時候被調用,在Activity創建的時候并不會被調用。

  theonStart() method is a good place to verify that required system features are enabled:(一般在onStart()方法里進行一些系統特殊是否可用的判斷)

  2)保存Activity中View的信息

  Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).  (比如在切屏的時候,Activity會銷毀,但Activity組件數據會消除怎么辦?)

  Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.(保存View的信息,要求View必須有一唯一的ID值)

  

  具體怎么保存View的狀態,View的狀態又會傳到哪兒去?

To save additional data about the activity state, you must override the onSaveInstanceState() callback method. The system calls this method when the user is leaving your activity and passes it the Bundle object that will be saved in the event that your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle object to both the onRestoreInstanceState() and onCreate()methods.

  在代碼里保存信息

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first
   
    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

 取出信息

public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);
   
    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}



關于activity ondestroy方法被莫明的調用的問題,手機上正常,平板會有此現象。

1)可能是activity在清單文件里沒有配置screenorientation="portriat"屬性,而平板一開始默認橫屏。但是在baseactivity里又通過代碼設置了豎屏,導致activity被重新創建。



向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

仙桃市| 金寨县| 阳高县| 望都县| 宿州市| 贵德县| 正阳县| 齐齐哈尔市| 曲靖市| 汝城县| 台南市| 玛曲县| 冷水江市| 安塞县| 宜州市| 福建省| 南华县| 淮安市| 玛曲县| 满城县| 当涂县| 会宁县| 清流县| 绩溪县| 明溪县| 徐汇区| 肇东市| 哈尔滨市| 泰宁县| 庐江县| 鹿泉市| 萍乡市| 浏阳市| 呼伦贝尔市| 兰溪市| 阳城县| 新绛县| 东乡族自治县| 涿鹿县| 青神县| 大余县|