使用意图在 Android 选项卡之间切换
我希望了解如何使用意图在选项卡之间切换。
在我的例子中,我使用两个选项卡:
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 活动作为独立页面获取,而不是在选项卡本身内。
知道那里出了什么问题吗?
I wish to find out how to switch between tabs using intents.
In my case I'm using the both tabs:
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);
To simplify my goal, my CaptureActivity.java includes the following code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.capture);
Intent intent = new Intent(this, ImageUpload.class);
startActivityForResult(intent, 0);
}
What I'm expecting is, the app should switch instantly to the second tab (ImageUpload activity) which works fine, BUT the tabs themselves disappear. I get the ImageUpload activity as stand-alone page, not within the tab itself.
Any idea what's going wrong there ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先调用触发 ImageUpload.java 只会触发 ImageUpload.class,但 tabhost 肯定会消失。
您需要触发 MainActivity-TabActivity,其中添加了两个 tabHost
1.ImageUpload
2.CaptureActivity ,
这将维护您的 TabLayout
调用意图,如下所示
现在主要活动已经在运行指针直接指向 Main Activity 内的 onNewIntent()
重写 MainActivity 内的 onNewIntent()
==================================== ===
First calling firing ImageUpload.java only fires ImageUpload.class but surely tabhost will disappear.
You need to fire your MainActivity-TabActivity where you added your two tabHost
1.ImageUpload
2.CaptureActivity
which will maintain your TabLayout
call intent like these
Now Main activity is already running so pointer directly goes to onNewIntent() inside Main Activity
Override onNewIntent() inside MainActivity
=====================================
在创建 tabhost 的父活动类中,我实现了如下所示的方法:
在选项卡内部,我希望能够在内部切换到另一个选项卡,我创建了以下方法:
如果您想要一个很好的示例这段代码,你可以看看我的MintTrack项目此处和 此处。
In the parent activity class where the tabhost is created I implemented a method like the one below:
Inside of the tab that I would like to be able to switch internally to another tab I created the method below:
If you would like a good example of this code, you can take a look at my MintTrack project here and here.