Android面試之Activity生命週期

在我們操作APP的時候,Activity之間會不斷進行切換,其中就包含了每一個Activity的生命週期,我們就可以根據生命週期來處理不同的業務。
因此這也是面試官喜歡問到的問題。

  • 簡單理解Activity的生命週期

這裏寫圖片描述
稍微看一下這圖,我相信寫Android的同學一定不會陌生,這張圖詳細地展示了Activity的不同狀態以及狀態的轉換,看着這圖我們也加點代碼來理解記住,有利於我們以後怎麼去描述出來什麼是Activity的生命週期。

public class MainActivity extends Activity {
    @Bind(R.id.button)
    Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        Log.i("TAG", "onCreate");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("TAG", "onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("TAG", "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("TAG", "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("TAG", "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("TAG", "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("TAG", "onDestroy");
    }

    @OnClick(R.id.button)
    public void jumpActivity() {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
    }
}

從啓動Activity到退出Activity的一個完整調用週期
09-13 18:06:05.296 5265-5265/com.example.pc.myapplication I/TAG: onCreate
09-13 18:06:05.297 5265-5265/com.example.pc.myapplication I/TAG: onStart
09-13 18:06:05.297 5265-5265/com.example.pc.myapplication I/TAG: onResume
09-13 18:06:07.727 5265-5265/com.example.pc.myapplication I/TAG: onPause
09-13 18:06:07.741 5265-5265/com.example.pc.myapplication I/TAG: onStop
09-13 18:06:07.741 5265-5265/com.example.pc.myapplication I/TAG: onDestroy

  • 管理Activity生命週期

    1. onCreate()
      -使用onCreate()初始化Activity:創建UI、綁定引用、加載數據等等。我們通常在onCreate()裏面寫initView()、initData()。
      -如果需要創建對象,建議它們在onCreate()進行創建,因爲在Activity的生命週期中它只會被調用一次。
      and:onCreate()裏面儘量少做事情,避免UI界面很久都加載不出來。

    2. onStart()
      -Activity可見時,會調用onStart()方法

    3. onResume()和onPause()
      -用戶可以獲取當前Activity的焦點,Activity在處於前臺狀態,會調用onResume(),那就是說第一次創建的時候和當用戶從onPaused()狀態恢復Activity時。因此onResume()和onPause()來配合進行對組件的初始化和釋放。
      -當Activity仍然處於部分可見的狀態,但是用戶不能對這個界面進行操作的時候,會調用onPause()。

例如:

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MobclickAgent.onResume(this);
        if (mMapView != null) {
            mMapView.setVisibility(View.VISIBLE);
            mMapView.onResume(); // 使百度地圖地圖控件和Fragment的生命週期保持一致
        }
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
         MobclickAgent.onPause(this);
        if (mMapView != null) {
            mMapView.setVisibility(View.INVISIBLE);
            mMapView.onPause(); // 使百度地圖地圖控件和Fragment的生命週期保持一致
        }
    }
  1. onStop()
    -當Activity調用onStop()方法,Activity處於不可見的狀態,此時當前的Activity停止。

  2. onDestroy()
    -銷燬,當Activity調用onDestory()方法我們通常會對我們使用的資源進行清理或者置空。


  • Activity事件方法回調

1.啓動Activity

09-13 18:56:24.359 26177-26177/com.example.pc.myapplication I/TAG: onCreate
09-13 18:56:24.360 26177-26177/com.example.pc.myapplication I/TAG: onStart
09-13 18:56:24.360 26177-26177/com.example.pc.myapplication I/TAG: onResume

2.退出Activity
09-13 18:57:04.597 26177-26177/com.example.pc.myapplication I/TAG: onPause
09-13 18:57:04.811 26177-26177/com.example.pc.myapplication I/TAG: onStop
09-13 18:57:04.811 26177-26177/com.example.pc.myapplication I/TAG: onDestroy

3.按Home鍵
09-13 19:38:49.998 26177-26177/com.example.pc.myapplication I/TAG: onPause
09-13 19:38:50.315 26177-26177/com.example.pc.myapplication I/TAG: onStop

4.重新啓動
09-13 19:49:27.945 26177-26177/com.example.pc.myapplication I/TAG: onRestart
09-13 19:49:27.946 26177-26177/com.example.pc.myapplication I/TAG: onStart
09-13 19:49:27.946 26177-26177/com.example.pc.myapplication I/TAG: onResume

5.跳轉新Activity
09-13 19:50:04.572 26177-26177/com.example.pc.myapplication I/TAG: onPause
09-13 19:50:05.052 26177-26177/com.example.pc.myapplication I/TAG: onStop

6.再回來舊的Activity
09-13 19:50:22.013 26177-26177/com.example.pc.myapplication I/TAG: onRestart
09-13 19:50:22.014 26177-26177/com.example.pc.myapplication I/TAG: onStart
09-13 19:50:22.014 26177-26177/com.example.pc.myapplication I/TAG: onResume

7,旋轉屏幕
09-13 20:16:33.687 27173-27173/com.example.pc.myapplication I/TAG: onPause
09-13 20:16:33.688 27173-27173/com.example.pc.myapplication I/TAG: onStop
09-13 20:16:33.688 27173-27173/com.example.pc.myapplication I/TAG: onDestroy
09-13 20:16:33.740 27173-27173/com.example.pc.myapplication I/TAG: onCreate
09-13 20:16:33.740 27173-27173/com.example.pc.myapplication I/TAG: onStart
09-13 20:16:33.742 27173-27173/com.example.pc.myapplication I/TAG: onResume


這就是我們需要的Activity的生命週期,大家通過簡單的Log來了解一下吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章