Android ActivityGroup实现的TabHost 保留activity原来状态

时间:2014-05-26 05:04:45   收藏:0   阅读:287

    在用ActivityGroup实现的TabHost时,由tab1切换到tab2再切换回来时,会重新加载tab1里的activity 

   

public class MainUIActivity extends  ActivityGroup{
 private Context context=this;
 private TabHost mTabhost;
 private String TAB1="首页";
 private String TAB2="收藏";
 private String TAB3="3";
 private String TAB4="4";
 private String TAB5="更多";
 
//定义数组来存放按钮图片
	private int mImageViewArray[] = {R.drawable.tab_home_btn,R.drawable.tab_message_btn,R.drawable.tab_selfinfo_btn,
									 R.drawable.tab_square_btn,R.drawable.tab_more_btn};
	
	//Tab选项卡的文字
	private String mTextviewArray[] = {TAB1, TAB2, TAB3, TAB4, TAB5};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_main);
		mTabhost=(TabHost) findViewById(R.id.tabhost1);
		mTabhost.setup();
		mTabhost.setup(this.getLocalActivityManager());
		Intent i1=new Intent(this,MainActivity.class);
		Intent i_collect=new Intent(this,CollectActivity.class);
//		Intent i1=new Intent(this,MainActivity.class);
//		Intent i1=new Intent(this,MainActivity.class);
//		Intent i1=new Intent(this,MainActivity.class);
		mTabhost.addTab(mTabhost.newTabSpec(TAB1).setIndicator(getTabItemView(0)).setContent(R.id.zhuye).setContent(i1));
		//注意,这里是newTabSpec(TAB2),每个tab选项卡要写的不一样,这样才能保存原来的状态
		mTabhost.addTab(mTabhost.newTabSpec(TAB2).setIndicator(getTabItemView(1)).setContent(R.id.zhuye2).setContent(i_collect));
		mTabhost.addTab(mTabhost.newTabSpec(TAB1).setIndicator(getTabItemView(2)).setContent(R.id.zhuye3).setContent(i1));
		mTabhost.addTab(mTabhost.newTabSpec(TAB1).setIndicator(getTabItemView(3)).setContent(R.id.zhuye4).setContent(i1));
		mTabhost.addTab(mTabhost.newTabSpec(TAB1).setIndicator(getTabItemView(4)).setContent(R.id.zhuye5).setContent(i1));
		
	}
	/**
	 * 给Tab按钮设置图标和文字
	 */
	private View getTabItemView(int index){
		View view = LayoutInflater.from(context).inflate(R.layout.tab_item, null);
	
		ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon);
		imageView.setImageResource(mImageViewArray[index]);
		
		TextView textView = (TextView) view.findViewById(R.id.tab_name);		
		textView.setText(mTextviewArray[index]);
	
		return view;
	}
	
	  public static Intent createIntent(Context context) {
	        Intent i = new Intent(context, MainActivity.class);
	        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        return i;
	    }
}
解决方法在上面的代码中已经写了,就是为每个tab选项卡设置不同的tag

Android ActivityGroup实现的TabHost 保留activity原来状态,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!