解决android安卓TabHost默认进入第一个选项卡onCreate的问题.顺序问题

	th = this.getTabHost();

		int tadid = 2;

		// 设置mCurrentTab为非-1,addtab时候不会进入setCurrentTab()
		try {
			Field idcurrent = th.getClass().getDeclaredField("mCurrentTab");
			idcurrent.setAccessible(true);
			idcurrent.setInt(th, -2);
		} catch (Exception e) {
			e.printStackTrace();
		}

		th.addTab(th.newTabSpec("myview1").setIndicator("myview111").setContent(new Intent(this, MyView1.class)));
		th.addTab(th.newTabSpec("myview2").setIndicator("myview222").setContent(new Intent(this, MyView2.class)));
		th.addTab(th.newTabSpec("myview3").setIndicator("myview333").setContent(new Intent(this, MyView3.class)));

		// 设置mCurrentTab与tadid不同,并且不能数组越界(0-2),保证第一次进入tab的setCurrentTab()方法正常运行
		try {
			Field idcurrent = th.getClass().getDeclaredField("mCurrentTab");
			idcurrent.setAccessible(true);
			if (tadid == 0) {
				idcurrent.setInt(th, 1);
			} else {
				idcurrent.setInt(th, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		th.setCurrentTab(tadid);


在我们使用TabHost 的时候,如果我们希望默认进入非第一个选项卡的时候,我们会这样写:tabHost.setCurrentTab(???);,但是,这样就出现了一个问题。我们发现无论进入哪个选项卡,在第一次进入TabHost的时候,都会调用第一个选项卡的 OnCreate方法!   这个不是我们想要的。  加上上面两个写注释的方法,即可解决问题!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值