用ActivityGroup解决TabHost中多个Activity跳转有关问题
用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>
1 楼
imagoodboy2005
2011-10-11
不错,好
2 楼
vip880628
2011-11-01
太好了。。。找一天了
3 楼
hkp2008
2011-11-02
vip880628 写道
太好了。。。找一天了
4 楼
SHuiX
2011-11-04
终于解决困扰了,
。。。
5 楼
467134382
2011-11-18
谢谢...留个QQ。。。
6 楼
Ron_luo
2012-02-01
顶上!
7 楼
Ron_luo
2012-02-01
群主问以下,按返回按钮的时候,怎样才能逐步返回,就是activity A-B-C-D;我要按D-C-B-A,返回,但是我现在一按就直接从D返回到A这个activity,请问有好办法吗?
8 楼
hkp2008
2012-02-01
Ron_luo 写道
群主问以下,按返回按钮的时候,怎样才能逐步返回,就是activity A-B-C-D;我要按D-C-B-A,返回,但是我现在一按就直接从D返回到A这个activity,请问有好办法吗?
可以使用栈来实现!
9 楼
daiziadai
2012-02-11
hkp2008 写道
Ron_luo 写道
群主问以下,按返回按钮的时候,怎样才能逐步返回,就是activity A-B-C-D;我要按D-C-B-A,返回,但是我现在一按就直接从D返回到A这个activity,请问有好办法吗?
可以使用栈来实现!
楼主可以详细说说吗,现在正好遇到这个问题
10 楼
datoushan
2012-02-23
非常感谢,研究这个问题很长时间了!
11 楼
JavaJ2me
2012-03-23
楼主能否具体说明一下,,如何返回上一个Activity呢??
12 楼
JavaJ2me
2012-03-23
实现了,,谢谢。。
13 楼
JavaJ2me
2012-03-23
这个事件是否也拦截了,手机上的Back事件啊,,
14 楼
ameryzhu
2012-04-25
Wonderful!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!