public class ModelGroupTab extends ActivityGroup { LinearLayout container; //用于管理本Group中的所有Activity public static ActivityGroup group; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.modeltabgroup); // container=(LinearLayout)findViewById(R.id.modeltabgroup); group = this; } @Override public void onBackPressed() { //把后退事件交给子Activity处理 group.getLocalActivityManager().getCurrentActivity().onBackPressed(); } @Override protected void onStart() { super.onStart(); //要跳转的Activity Intent intent = new Intent(this, A_Activity.class); // 把Activity转换成一个Window,然后转换成View Window w = group.getLocalActivityManager().startActivity( "FirstActivity", intent); View view = w.getDecorView(); //container.removeAllViews(); //container.addView(view); //设置要跳转的Activity显示为本ActivityGroup的内容 group.setContentView(view); } }
把按返回按钮的事件交给子activity处理
初始化要跳转到的activity
LocalActivityManager通过startActivity(String id, Intent intent),可以与指定的Actiivty绑定,
@Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(A_Activity.this, B_Activity.class); // 把Activity转换成一个Window,然后转换成View Window w = ModelGroupTab.group.getLocalActivityManage().startActivity("BActivity",intent); View view = w.getDecorView(); LinearLayout container=(LinearLayout) ModelGroupTab.group.getWindow().findViewById(R.id.modeltabgroup); //设置要跳转的Activity显示为本ActivityGroup的内容 ModelGroupTab.group.setContentView(view); }
Aactivity跳转到Bactivity
@Override public void onBackPressed() { // TODO Auto-generated method stub //super.onBackPressed(); Intent intent = new Intent(B_Activity.this, A_Activity.class); // 把Activity转换成一个Window,然后转换成View String id=ModelGroupTab.group.getLocalActivityManager() .getCurrentId(); Window w = ModelGroupTab.group.getLocalActivityManager() .startActivity("A_Activity",intent); View view = w.getDecorView(); //设置要跳转的Activity显示为本ActivityGroup的内容 ModelGroupTab.group.setContentView(view); System.out.println("activity "+id+" finsih"); ModelGroupTab.group.getLocalActivityManager().destroyActivity(id, true); }
Bactivity回退到Aactivity,覆写
public void onBackPressed()
写完设置跳转的代码后,finish当前activity,先获得当前绑定的activity id,然后通过
ModelGroupTab.group.getLocalActivityManager().destroyActivity(id, true);销毁