我希望了解如何使用意图在标签之间切换。
在我的情况下,我正在使用两个标签:
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
// Capture tab
spec = tabHost.newTabSpec("capture").setIndicator(null,
res.getDrawable(R.drawable.ic_tab_capture))
.setContent(new Intent(this,CaptureActivity.class));
tabHost.addTab(spec);
// Upload tab
spec = tabHost.newTabSpec("upload").setIndicator(null,
res.getDrawable(R.drawable.ic_tab_capture))
.setContent(new Intent(this,ImageUpload.class));
tabHost.addTab(spec);
为了简化我的目标,我的CaptureActivity.java包含以下代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.capture);
Intent intent = new Intent(this, ImageUpload.class);
startActivityForResult(intent, 0);
}
我期待的是,应用应立即切换到第二个标签(ImageUpload活动),它可以正常工作,但标签本身也会消失。我将ImageUpload活动作为独立页面获取,而不是在选项卡本身内。
知道那里出了什么问题吗?
答案 0 :(得分:1)
第一次调用激活ImageUpload.java只会触发ImageUpload.class,但是tabhost肯定会消失。
您需要在添加两个tabHost
的位置激活MainActivity-TabActivity<强> 1.ImageUpload 强>
<强> 2.CaptureActivity 强>
将维护您的TabLayout
像这些一样打电话Intent i=new Intent(getApplicationContext(),MainActivity.class)//which is your mainActivity-Launcher
i.addFlags(Intent.FLAG_ACTIVITY_BRING_TO_FRONT);
startActivity(i);//will bring MainACtivity to Front
现在Main活动已经在运行,因此指针直接转到Main Activity
中的onNewIntent()在MainActivity中覆盖onNewIntent()
=====================================
public class MainActivity extends TabActivty
{
pubilc static TabHost tabhost;
public void onCreate()
{
//where you added your two tab spec
//added them
}
public void onNewIntent(intent)
{
super.onNewIntent(intent);
tabHost.setCurrentTab(1);//1-depends where you want to switch-tabIndexno, I assume Upload is at 2nd position (so "1")
Activity currentActivity=getCurrentActivity();
if(currentActivity instanceof Upload)
{
((upload)currentActivity).onCreate();//watever method you want to call
}
}
}
答案 1 :(得分:0)
在创建tabhost的父活动类中,我实现了如下所示的方法:
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
在我希望能够在内部切换到另一个标签的标签内,我创建了以下方法:
public void switchTabInActivity(int indexTabToSwitchTo){
YOURTABHOSTACTIVITY ParentActivity;
ParentActivity = (YOURTABHOSTACTIVITY) this.getParent();
ParentActivity.switchTab(indexTabToSwitchTo);
}