问题: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