Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android ActivityGroup實現的TabHost 保留activity原來狀態

Android ActivityGroup實現的TabHost 保留activity原來狀態

編輯:關於Android編程

在用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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved