我有一个带有 4 个选项卡的 TabHost 应用程序。
选项卡 1 列表 A > 列表 B > 查看
Tab 2 Camera Activity 启动相机并闪烁 onCreate
选项卡 3 列表 C > 查看
选项卡 4 视图 D > 视图 E
所以,当我第一次启动应用程序时,我可以毫无问题地在堆栈中来回切换不同的选项卡。但是,一旦我启动 Tab 2(相机选项卡)并从那里转到其他选项卡并向下堆栈,当我按下后退按钮时,即使我不在选项卡 2(即:在列表 B,按下后退按钮,即使我只是返回列表 A,相机也会启动...当我再次转到选项卡 2 时,应用程序将崩溃(抱歉,Android 相机遇到问题...等等)
谁能告诉我问题是什么?以下是我的 TabHost 活动
package com.myapp.appname;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.view.Window;
public class MainActivity extends TabActivity {
/** Called when the activity is first created. */
private TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.top_titlebar);
setTabs();
}
private void setTabs() {
tabHost = getTabHost();
listTab(R.string.tab_1, R.drawable.ic_tab_1);
cameraTab(R.string.tab_2, R.drawable.ic_tab_2);
list2Tab(R.string.tab_3, R.drawable.ic_tab_3);
viewTab(R.string.tab_4, R.drawable.ic_tab_4);
}
private void listTab(int labelId, int drawableId) {
Intent intent = new Intent(this,ListActivity.class);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
private void cameraTab(int labelId, int drawableId) {
Intent intent = new Intent(this,CameraActivity.class);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
private void list2Tab(int labelId, int drawableId) {
Intent intent = new Intent(this,List2Activity.class);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
private void viewTab(int labelId, int drawableId) {
Intent intent = new Intent(this,ViewActivity.class);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}