android activitygroup 使用
创建一个activity 将返回键的处理交给子activity处理。
public class DailyKaoshiActivityGroup extends ActivityGroup{
/**
* 一个静态的ActivityGroup变量,用于管理本Group中的Activity
*/
public static ActivityGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
group = this;
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
// super.onBackPressed();
//把后退事件交给子Activity处理
group.getLocalActivityManager()
.getCurrentActivity().onBackPressed();
}
@SuppressWarnings("deprecation")
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//把界面切换放到onResume方法中是因为,从其他选项卡切换回来时,
//调用搞得是onResume方法
//要跳转的界面,跳转的第一个activity
Intent intent = new Intent(this, DailyKaoShiActivity.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//把一个Activity转换成一个View
Window w = group.getLocalActivityManager().startActivity("DailyKaoShiActivity",intent);
View view = w.getDecorView();
//把View添加大ActivityGroup中
group.setContentView(view);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//将按键的处理交给相应的子activity处理
return group.getLocalActivityManager().getCurrentActivity().onKeyDown(keyCode, event);
}
}
DailyKaoShiActivity.java中涉及的关键代码:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putSerializable("shitidata", shitiDatas);
intent.putExtras(bundle);
intent.setClass(DailyKaoShiActivity.this, DailyShiTiAtivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//把一个Activity转换成一个View
Window w = DailyKaoshiActivityGroup.group.getLocalActivityManager()
.startActivity("DailyShiTiAtivity",intent);
View view = w.getDecorView();
//把View添加大ActivityGroup中
DailyKaoshiActivityGroup.group.setContentView(view);