ActivityGroup和TabActivity的搭配使用
1.用途
如果你有一个AActivity是在TabActivity的tab中的,然后想跳转到BActivity,并且当前的tab页面还是要保留显示的,就可以使用ActivityGroup
2.使用介绍
首先你要有一个类继承ActivityGroup,这个类叫做ParentActivity,然后把ParentActivity加入到你的TabActivity的tab中去,然后在ParentActivity的layout中定义LinearLayout,要把LinearLayout的id加上,id可以随便写的,后面会用到。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/myactivitygoup_view" > </LinearLayout>
然后在ParentActivity的onCreate方法中调用initView方法进行跳转到AActivity,我这个自己在Util类中封装了一个gotoNextActivity的方法
注意:ParentActivity也要在Manifest文件中注册,因为这也是一个Activity.
这个是在ParentActivity里的initView()方法,里面调用了gotoNextActivity()方法。
private void initView() { Intent intent=new Intent(ParentActivity.this,AActivity.class); Util.gotoNextActivity(this, intent, "AActivity"); }
这个是Util类中的gotoNextActivity方法
public static void gotoNextActivity(ActivityGroup group,Intent intent,String nextName) { LinearLayout container = (LinearLayout) group.getWindow().findViewById(R.id.myactivitygoup_view);// 注意这里,还是获取group的view container.removeAllViews(); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//跳转到下一个activity //intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); Window subActivity = group.getLocalActivityManager().startActivity(nextName, intent); View view = subActivity.getDecorView(); container.addView(view); LinearLayout.LayoutParams params = (LayoutParams) view.getLayoutParams(); params.width = LayoutParams.FILL_PARENT; params.height = LayoutParams.FILL_PARENT; view.setLayoutParams(params); }
注意:使用gotoNextActivity的时候需要一个ActivityGroup参数。因为ParentActivity是ActivityGroup的子类所以可以使用this。AAcitivity跳转到BAcitivity的时候,可以这样。
Intent intent=new Intent(AActivity.this,BActivity.class); Util.gotoNextActivity((ActivityGroup)getParent(), intent, "BActivity");
下面是回退的方法
public static void gotoBackActivity(ActivityGroup group,Intent intent,String backName) { LinearLayout container = (LinearLayout)group.getWindow().findViewById(R.id.myactivitygoup_view); container.removeAllViews(); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//回调到前一个activity intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); Window subActivity = group.getLocalActivityManager().startActivity(backName, intent); View view = subActivity.getDecorView(); container.addView(view); LinearLayout.LayoutParams params = (LayoutParams) view.getLayoutParams(); params.width = LayoutParams.FILL_PARENT; params.height = LayoutParams.FILL_PARENT; view.setLayoutParams(params); }
然后在BActivity中调用
Intent intent=new Intent(BActivity.this,AActivity.class); Util.gotoBackActivity((ActivityGroup)getParent(), intent, "AActivity");
注意:在回退和跳转的时候不用调用finish()方法,不然整个ActivitGroup都会结束的。
3.使用ActivityGroup遇到的一些问题
1.在ParentActivity的子项AAcitivity中调用AlertDialog,没有反应。
解决方法:在创建AlerttDialogBuilder的要是使用getParent()
AlertDialog.Builder builder=new AlertDialog.Builder((ActivityGroup)getParent());
2.在AAcitivity中不能监听到返回键事件。
解决方法:
在ParentActivity中重写dispatchKeyEvent方法
<span style="font-size:12px;">@Override public boolean dispatchKeyEvent(KeyEvent event) { // TODO Auto-generated method stub if(event.getAction()==KeyEvent.ACTION_DOWN&&event.getKeyCode()==KeyEvent.KEYCODE_BACK){ // getLocalActivityManager().getActivity(CURID).onKeyDown(event.getKeyCode(), event); return getLocalActivityManager().getCurrentActivity().onKeyDown(event.getKeyCode(), event); } return super.dispatchKeyEvent(event); }</span>
然后在AActivity中重写onKeyDown方法
<span style="font-size:12px;">@Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(event.getKeyCode()==KeyEvent.KEYCODE_BACK){ // getLocalActivityManager().getActivity(CURID).onKeyDown(event.getKeyCode(), event); AlertDialog.Builder builder=new AlertDialog.Builder((ActivityGroup)getParent()); builder.setTitle("提示"); builder.setMessage("确定要退出程序吗?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub android.os.Process.killProcess(android.os.Process.myPid()); // 结束进程 } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); builder.show(); return false; } return super.onKeyDown(keyCode, event); } </span>
3.在AActivity中使用startActivitForResult到BActivity后,在onActivityResult()方法中接收不到BActivity的反馈。
解决方法:
在ParentActivity中重写onActivityResult()方法,在AActivity中自己定义一个handleActivityResult的方法
<span style="font-size:14px;"> @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(requestCode==0){ AActivity activity=(AActivity)getLocalActivityManager().getCurrentActivity(); activity.handleActivityResult(requestCode, resultCode, data);//把收到的消息发送给发起请求的Activity AActivity } }</span>
这个是handleActivityResult方法,然后就可以在这个方法里面进行一些操作
<span style="font-size:14px;">public void handleActivityResult(int requestCode, int resultCode, Intent data){ if(resultCode == RESULT_OK){//获取返回码,刷新界面 Log.i("MXH", "返回码:"+resultCode); } </span>
4.在AActivity中无法监听到menu键
解决方法:
在ParentActivity中重写dispatchKeyEvent方法
<span style="font-size:12px;">@Override public boolean dispatchKeyEvent(KeyEvent event) { if(event.getAction()==KeyEvent.ACTION_DOWN&&event.getKeyCode()==KeyEvent.KEYCODE_MENU) { getLocalActivityManager().getCurrentActivity().openOptionsMenu(); } return super.dispatchKeyEvent(event); }</span>