用ActivityGroup解决TabHost中多个Activity跳转问题
最近在做一个程序,刚开始没考虑全,就用TabHost做了,后来才发现程序中,需要在一个TabHost内实现多个Activity的跳转,网上搜了一翻,有人建议把TabHost改成Button,然后每个Activity中都处理加入的Button,这样是可以解决问题,但是修改起来很繁琐,所以还是继续寻找替代方法。在网上搜到了《使用ActivityGroup来切换Activity和Layout》一文,但是用在我的程序中还需要有大的改动,所以索性我就自己写了个测试例子,不错,成功了,拿出来和大家分享一下,希望对大家有帮助!
下面图片是测试程序的效果图

两个选项卡的实现
布局文件 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="3">
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Java代码实现 MainActivity.java
package hkp.test;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("First")
.setContent(new Intent(this,FirstGroupTab.class)));//第一个选项卡使用一个ActivityGroup
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("Second")
.setContent(new Intent(this, SecondTab.class)));//第二个选项卡是一个Activity
tabHost.setCurrentTab(0);
}
}
使用 ActivityGroup的管理
package hkp.test;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
/**
* @author HuangKaipeng hkp2006@gmail.com
* 2011-10-5
*
*/
public class FirstGroupTab 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();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//把界面切换放到onResume方法中是因为,从其他选项卡切换回来时,
//调用搞得是onResume方法
//要跳转的界面
Intent intent = new Intent(this, FirstActivity.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//把一个Activity转换成一个View
Window w = group.getLocalActivityManager().startActivity("FirstActivity",intent);
View view = w.getDecorView();
//把View添加大ActivityGroup中
group.setContentView(view);
}
ActivityGroup中的第一个Activity
package hkp.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @author HuangKaipeng hkp2006@gmail.com
* 2011-10-5
*
*/
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
//跳转到第二个界面
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(FirstActivity.this, SecondActivity.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//把一个Activity转换成一个View
Window w = FirstGroupTab.group.getLocalActivityManager()
.startActivity("SecondActivity",intent);
View view = w.getDecorView();
//把View添加大ActivityGroup中
FirstGroupTab.group.setContentView(view);
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这个是ActivityGroup中的第一个界面!"
/>
<Button android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="跳转到本组中的另一个Activity中"/>
</LinearLayout>
16楼:里面放一个确认对话框AlertDialog会报错:Uncaught handler: thread main exiting due to uncaught exception 解决了吗
我也遇到同样的问题,求解释啊
返回事件好像被屏蔽了还是怎么的,里面放一个确认对话框AlertDialog会报错:Uncaught handler: thread main exiting due to uncaught exception
学习了,多谢
Wonderful!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
这个事件是否也拦截了,手机上的Back事件啊,,
实现了,,谢谢。。
楼主能否具体说明一下,,如何返回上一个Activity呢??
非常感谢,研究这个问题很长时间了!
楼主可以详细说说吗,现在正好遇到这个问题
可以使用栈来实现!
群主问以下,按返回按钮的时候,怎样才能逐步返回,就是activity A-B-C-D;我要按D-C-B-A,返回,但是我现在一按就直接从D返回到A这个activity,请问有好办法吗?
顶上!
谢谢...留个QQ。。。 :oops:
终于解决困扰了, :idea: 。。。
:D 哈哈,希望对你有用,请多多关注我的博客哦!
太好了。。。找一天了 :cry:
不错,好