按下后退键,它从头开始加载最后一个活动

时间:2013-07-25 07:56:30

标签: android tabbar android-tabactivity

我正在开发一个Android应用程序,它基于底部的标签栏。我有四个父标签活动,如A,B,C,D。所有这些活动都继承了TabGroupActivity,而TabGroupActivity继承了ActivityGroup。一切都工作正常,但当我按回Android设备的后退键,它从头开始加载最后一个活动。我很想知道大多数人都有类似的问题,我无法得到解决方案。

我创建了以下课程

TabGroupActivity.java

public class TabGroupActivity extends ActivityGroup {

    private ArrayList<String> mIdList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);       
        if (mIdList == null) mIdList = new ArrayList<String>();
    }


  @Override
  public void finishFromChild(Activity child) {
      LocalActivityManager manager = getLocalActivityManager();
      int index = mIdList.size()-1;

      if (index < 1) {
          finish();
          return;
      }

      manager.destroyActivity(mIdList.get(index), true);
      mIdList.remove(index); index--;
      String lastId = mIdList.get(index);
      Intent lastIntent = manager.getActivity(lastId).getIntent();
      Window newWindow = manager.startActivity(lastId, lastIntent);
      setContentView(newWindow.getDecorView());
  }


  public void startChildActivity(String Id, Intent intent) {     
      Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
      if (window != null) {
          mIdList.add(Id);
          setContentView(window.getDecorView()); 
      }    
  }


  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
          return true;
      }
      return super.onKeyDown(keyCode, event);
  }


  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          onBackPressed();
          return true;
      }
      return super.onKeyUp(keyCode, event);
  }


  @Override
  public void  onBackPressed  () {
      int length = mIdList.size();
     // if ( length > 1) {
          Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
          current.finish();
      //}  
  }
}

我的每个tabgroup都继承了TabGroupAcitvity

HomeTabGroup.java

public class HomeTabGroup extends TabGroupActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startChildActivity("HomeActivity", new Intent(this,HomeActivity.class));
    }
}

0 个答案:

没有答案