onBackPressed導致FragmentActivity重啓

問題:onBackPressed導致FragmentActivity重啓

描述:

HomeActivity和MyTsyActivity是FragmentActivity的兩個子類。

從HomeActivity跳轉到MyTsyActivity很流暢,沒有出現任何問題。

Intent intent = new Intent(context, MyTsyActivity.class);
startActivity(intent);

從MyTsyActivity返回到HomeActivity中有兩種方法

(1)點擊標題欄的返回按鈕,在返回按鈕的clickListener中的onClick方法中調用this.finish()

public void onClick(View v) {
    if(v==btnBack) {
        finish();
    }
}

結果是先調用HomeActivity的onStart方法,再調用MyTsyActivity的onDestroy方法。

(2)點擊pad底部的返回按鍵,觸發MyTsyActivity的onBackPressed方法

@Override
public void onBackPressed() {
    Logger.v(TAG, "@onBackPressed.");
    super.onBackPressed();
}

結果會重啓HomeActivity,導致返回去比較卡,不流暢。但也不是每次都會重啓。

打印出來的日誌如下:

11-26 16:46:52.860: V/MyTsyActivity(32384): @onBackPressed.
11-26 16:46:53.190: V/HomeActivity(32384): @onDestroy.
11-26 16:46:53.190: V/HomeActivity(32384): @stopService.
11-26 16:46:53.200: V/MainFragment(32384): @onDestroyView
11-26 16:46:53.200: V/HotVideoFragment(32384): @onDestroyView
11-26 16:46:53.200: V/HotVideoFragment(32384): @onDestroy
11-26 16:46:53.200: V/RecommandFragment(32384): @onDestroyView
11-26 16:46:53.200: V/RecommandFragment(32384): @onDestroy
11-26 16:46:53.200: V/HotVideoFragment(32384): @onDestroyView
11-26 16:46:53.200: V/HotVideoFragment(32384): @onDestroy
11-26 16:46:53.200: V/RecommandFragment(32384): @onDestroyView
11-26 16:46:53.200: V/RecommandFragment(32384): @onDestroy
11-26 16:46:53.200: V/HotVideoFragment(32384): @onDestroyView
11-26 16:46:53.200: V/HotVideoFragment(32384): @onDestroy
11-26 16:46:53.200: V/RecommandFragment(32384): @onDestroyView
11-26 16:46:53.200: V/RecommandFragment(32384): @onDestroy
11-26 16:46:53.210: V/HomeActivity(32384): @onCreate.
11-26 16:46:53.210: V/MainFragment(32384): @onCreate
11-26 16:46:53.210: V/HotVideoFragment(32384): @onCreate
11-26 16:46:53.210: V/RecommandFragment(32384): @onCreate
11-26 16:46:53.210: V/HotVideoFragment(32384): @onCreate
11-26 16:46:53.210: V/RecommandFragment(32384): @onCreate
11-26 16:46:53.210: V/HotVideoFragment(32384): @onCreate
11-26 16:46:53.210: V/RecommandFragment(32384): @onCreate
11-26 16:46:53.250: V/HomeActivity(32384): @launchService.
11-26 16:46:53.250: V/MainFragment(32384): @onCreateView
11-26 16:46:53.260: V/MainFragment(32384): @initializeView. create each fragment.
11-26 16:46:53.260: V/MainFragment(32384): @onActivityCreated. data is null. get data from network.
11-26 16:46:53.270: V/HotVideoFragment(32384): @onCreateView
11-26 16:46:53.290: V/RecommandFragment(32384): @onCreateView
11-26 16:46:53.290: V/HotVideoFragment(32384): @onCreateView

(3)如果使用onKeyDown來處理返回按鍵,也會出現FragmentActivity重啓的問題。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        Logger.v(TAG, "@onKeyDown. keyCode is KeyEvent.KEYCODE_BACK");
        this.finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

在onKeyDown中,如果是KEYCODE_BACK,返回true,表示不再執行onBackPressed方法。

打印出來的日誌跟調用onBackPressed類似。

多嘗試了幾次,發現onKeyDown和onBackPressed兩個方法引起Activity重啓不是每次都發生。其中有一次的現象是:前4、5次都沒有重啓,再來一次就重啓。

在FragmentActivity.onBackPressed文檔說明中發現:

Take care of popping the fragment back stack or finishing the activity as appropriate.

沒有明白這句話具體意思。

參考資料:

1. ANDROID API:FragmentActivity

http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html


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