2013.03.19(2)———android ActivityGroup的使用
参考:
http://hkp.iteye.com/blog/1185482
场景:现在我需要在tab里面进行activity的跳转,但是tab工具栏仍然要显示,也就是一个tab的内部activity跳转
平常写的tab,不管是button、gridview或者radiogroup,在对应的activity点击跳转之后,就直接跳出tab了 ,然后 我就google了下,发现大家都说用ActivityGroup,我就参见上面的链接,写了一个简单例子
1、tab
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" />
<TabWidget
android:id="@android:id/tabs"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
<RadioGroup
android:id="@+id/more_tab_radiogroup"
android:gravity="center_vertical"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:padding="2dip"
android:background="@drawable/tab_background"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/more_tab_rdo_my"
android:text="@string/tab_my"
android:drawableTop="@drawable/tab_icon_my"
style="@style/tab_item_background"
android:checked="true"
/>
<RadioButton
android:id="@+id/more_tab_rdo_attention"
android:text="@string/tab_attention"
android:drawableTop="@drawable/tab_icon_attention"
style="@style/tab_item_background" />
<RadioButton
android:id="@+id/more_tab_rdo_camera"
android:text="@string/tab_camera"
android:drawableTop="@drawable/tab_icon_camera"
style="@style/tab_item_background" />
<RadioButton
android:id="@+id/more_tab_rdo_piazza"
android:text="@string/tab_piazza"
android:drawableTop="@drawable/tab_icon_piazza"
style="@style/tab_item_background" />
<RadioButton
android:id="@+id/more_tab_rdo_more"
android:text="@string/tab_more"
android:drawableTop="@drawable/tab_icon_more"
style="@style/tab_item_background" />
</RadioGroup>
</LinearLayout>
</TabHost>
MainTabActivity.java
package com.wmyc.activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
public class MainTabActivity extends TabActivity implements BaseInterface,OnCheckedChangeListener{
//================log标记=====================
private final static String TAG = MainTabActivity.class.getSimpleName();
//================一些常量=====================
private final static int TAB_MY = 0;
private final static int TAB_ATTENTION = 1;
private final static int TAB_CAMERA = 2;
private final static int TAB_PIAZZA = 3;
private final static int TAB_MORE = 4;
//================界面相关=====================
private Context _context;
private RadioGroup mRadioGroup;
private TabHost mTabHost;
//================逻辑相关=====================
//当前选中的tab
private int mCurrentIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab);
this.initVars();
this.initComponent();
this.loadData();
}
@Override
public void initVars() {
_context = this;
mCurrentIndex = TAB_MY;
}
@Override
public void initComponent() {
mRadioGroup = (RadioGroup)findViewById(R.id.more_tab_radiogroup);
mRadioGroup.setOnCheckedChangeListener(this);
initTabHost();
}
@Override
public void loadData() {
}
private void initTabHost() {
mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab my").setIndicator("")
.setContent(new Intent(this, FirstGroupTab.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab attention").setIndicator("")
.setContent(new Intent(this, SecondTab.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab camera").setIndicator("")
.setContent(new Intent(this, SecondTab.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab piazza").setIndicator("")
.setContent(new Intent(this, SecondTab.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab more").setIndicator("")
.setContent(new Intent(this, SecondTab.class)));
mTabHost.setCurrentTab(mCurrentIndex);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.more_tab_rdo_my:
mCurrentIndex = TAB_MY;
break;
case R.id.more_tab_rdo_attention:
mCurrentIndex = TAB_ATTENTION;
break;
case R.id.more_tab_rdo_camera:
mCurrentIndex = TAB_CAMERA;
break;
case R.id.more_tab_rdo_piazza:
mCurrentIndex = TAB_PIAZZA;
break;
case R.id.more_tab_rdo_more:
mCurrentIndex = TAB_MORE;
break;
}
mTabHost.setCurrentTab(mCurrentIndex);
}
}
2、FirstTabGroup.java
/**
*
*/
package com.wmyc.activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
/**
* @author HuangKaipeng [email protected]
* 2011-10-5
*
*/
public class FirstGroupTab extends ActivityGroup {
/**
* 一个静态的ActivityGroup变量,用于管理本Group中的Activity
*/
public static ActivityGroup group;
public static int currentIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("======FirstGroupTab onCreate=============");
group = this;
currentIndex = 1;
}
@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();
System.out.println("======FirstGroupTab onResume=============");
//把界面切换放到onResume方法中是因为,从其他选项卡切换回来时,
//调用搞得是onResume方法
//要跳转的界面
Intent intent = null;
if(currentIndex==1){
intent = new Intent(this,FirstActivity.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}else{
intent = new Intent(this,SecondActivity.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
//把一个Activity转换成一个View
Window w = group.getLocalActivityManager().startActivity(""+currentIndex,intent);
View view = w.getDecorView();
//把View添加大ActivityGroup中
group.setContentView(view);
}
@Override
protected void onPause() {
super.onPause();
System.out.println("======FirstGroupTab onPause=============");
}
@Override
protected void onStop() {
super.onStop();
System.out.println("======FirstGroupTab onStop=============");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("======FirstGroupTab onDestroy=============");
}
}
3、FirstActivity
/**
*
*/
package com.wmyc.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
/**
* @author HuangKaipeng [email protected]
* 2011-10-5
*
*/
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("======FirstActivity onCreate=============");
setContentView(R.layout.first_activity);
Bundle data = getIntent().getExtras();
// System.out.println("result: " + data.getInt("aaa"));
//跳转到第二个界面
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);
//把一个Activity转换成一个View
Window w = MyActivityGroup._group.getLocalActivityManager()
.startActivity(MyActivityGroup.STR_PRE+ 3,intent);
View view = w.getDecorView();
Animation hyperspaceJump = AnimationUtils.loadAnimation(FirstActivity.this,
R.anim.translucent_exit);
FirstActivity.this.getWindow().getDecorView().startAnimation(hyperspaceJump);
hyperspaceJump = AnimationUtils.loadAnimation(FirstActivity.this,
R.anim.translucent_enter);
view.startAnimation(hyperspaceJump);
//把View添加大ActivityGroup中
MyActivityGroup._group.setContentView(view);
MyActivityGroup.currentIndex = 3;
}
});
}
@Override
protected void onStart() {
super.onStart();
System.out.println("======FirstActivity onStart=============");
}
@Override
protected void onRestart() {
super.onRestart();
System.out.println("======FirstActivity onRestart=============");
}
@Override
protected void onResume() {
super.onResume();
System.out.println("======FirstActivity onResume=============");
}
@Override
protected void onPause() {
super.onPause();
System.out.println("======FirstActivity onPause=============");
}
@Override
protected void onStop() {
super.onStop();
System.out.println("======FirstActivity onStop=============");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("======FirstActivity onDestroy=============");
}
}
4、SecondActivity.java
/**
*
*/
package com.wmyc.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
/**
* @author HuangKaipeng [email protected]
* 2011-10-5
*
*/
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("======SecondActivity onCreate=============");
setContentView(R.layout.second_activity);
Button btn = (Button)this.findViewById(R.id.second_btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, MyMainActivity.class);
Window w = MyActivityGroup._group.getLocalActivityManager()
.startActivity(MyActivityGroup.STR_PRE + 1,intent);
View view = w.getDecorView();
MyActivityGroup._group.setContentView(view);
MyActivityGroup._group.getLocalActivityManager().destroyActivity(MyActivityGroup.STR_PRE + MyActivityGroup.currentIndex, true);
MyActivityGroup.currentIndex = 1;
}
});
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
// super.onBackPressed();
//后退跳转到第一个Activity界面
Intent intent = new Intent(this, MyMainActivity.class);
Window w = MyActivityGroup._group.getLocalActivityManager()
.startActivity(MyActivityGroup.STR_PRE + 1,intent);
View view = w.getDecorView();
MyActivityGroup._group.setContentView(view);
MyActivityGroup.currentIndex = 1;
}
@Override
protected void onStart() {
super.onStart();
System.out.println("======SecondActivity onStart=============");
}
@Override
protected void onRestart() {
super.onRestart();
System.out.println("======SecondActivity onRestart=============");
}
@Override
protected void onResume() {
super.onResume();
System.out.println("======SecondActivity onResume=============");
}
@Override
protected void onPause() {
super.onPause();
System.out.println("======SecondActivity onPause=============");
}
@Override
protected void onStop() {
super.onStop();
System.out.println("======SecondActivity onStop=============");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("======SecondActivity onDestroy=============");
}
}
SecondTab.java 就不写了,很简单一个页面