这是我的应用程序结构:我有一个带有4个选项卡的主"活动"(Account、Dashboard、Medias、Applications)。每个选项卡都链接到一个"活动"。"应用程序"选项卡包含许多"活动",为了使选项卡保持可见,我在ApplicationsActivity中执行if/else,以了解我必须显示的布局(可能不是最好的方法,但我没有找到更好的方法)。
我的问题是:当我进入"应用程序"选项卡时,我会看到所有应用程序的列表,然后我单击一个应用程序,我会得到一个新屏幕,其中包含该应用程序的选项,我会单击一个选项,然后我会得到新屏幕,等等。然后我点击媒体标签,我得到了所有媒体的列表。然后我再次单击"应用程序"选项卡,我仍然在屏幕上显示特定应用程序的选项。我想要的是回到所有应用程序的列表中。
我该怎么做?我该如何告诉选项卡它必须重新加载活动,而不是将其放入看似缓存的内容中?
这是我的主要活动代码:
public class MainActivity extends TabActivity {
private int currentTab = 1;
private Application application = null;
private String optionType = null;
private String checkID = null;
private String alertID = null;
private int periodID = -1;
private Statistic stat = null;
private Media media = null;
TabHost tabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
PreferencesManager.load(this);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
Log.i("IV", bundle.toString());
String applicationID = null;
applicationID = bundle.getString("applicationID");
if (applicationID != null) {
try {
this.application = Utils.getApplication(applicationID);
} catch (AuthenticationFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ApiCallErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InternalErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
this.application = (Application) bundle
.getSerializable("application");
}
this.currentTab = bundle.getInt("currentTab", 1);
this.optionType = bundle.getString("optionType");
this.checkID = bundle.getString("checkID");
this.alertID = bundle.getString("alertID");
this.periodID = bundle.getInt("periodID", -1);
this.stat = (Statistic) bundle.getSerializable("stat");
this.media = (Media) bundle.getSerializable("media");
}
Log.i("IV", "current tab : " + currentTab);
Resources res = getResources(); // Resource object to get Drawables
tabHost = getTabHost(); // The activity TabHost
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
//Log.i("IV", "Tab id : " +tabId);
setTabColor();
}
});
TabHost.TabSpec spec; // Resusable TabSpec for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
Intent intent = new Intent().setClass(this, AccountActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost
.newTabSpec("account")
.setIndicator(getString(R.string.account),
res.getDrawable(R.drawable.tab_account))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, DashboardActivity.class);
spec = tabHost
.newTabSpec("dashboard")
.setIndicator(getString(R.string.dashboard),
res.getDrawable(R.drawable.tab_dashboard))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MediasActivity.class).putExtra(
"media", media);
spec = tabHost
.newTabSpec("medias")
.setIndicator(getString(R.string.medias),
res.getDrawable(R.drawable.tab_media))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ApplicationsActivity.class)
.putExtra("application", application)
.putExtra("optionType", optionType)
.putExtra("checkID", checkID).putExtra("alertID", alertID)
.putExtra("periodID", periodID).putExtra("stat", stat);
spec = tabHost
.newTabSpec("applications")
.setIndicator(getString(R.string.applications),
res.getDrawable(R.drawable.tab_application))
.setContent(intent);
tabHost.addTab(spec);
setTabColor();
if (!PreferencesManager.getBoolean("isLogged")) {
for (int i = 1; i < 4; i++) {
tabHost.getTabWidget().getChildTabViewAt(i).setEnabled(false);
tabHost.getTabWidget().getChildTabViewAt(i)
.setBackgroundColor(Color.rgb(102, 102, 102));
}
tabHost.setCurrentTab(0);
} else {
tabHost.setCurrentTab(currentTab);
}
}
public void setTabColor() {
for(int i=0;i<this.tabHost.getTabWidget().getChildCount();i++)
{
this.tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#000000")); //unselected
}
this.tabHost.getTabWidget().getChildAt(this.tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#ABABAB")); // selected
}
}
在我的ApplicationsActivity中,我只对捆绑包中的值执行if/else操作,以了解在setContentView()中使用什么布局;
非常感谢!
我不知道;我不知道如果你理解你的问题,但也许可以尝试在android清单文件中更改你的活动设置并添加:
android:noHistory="true"
您是否曾尝试在TabActivity中使用ActivityGroup?有了ActivityGroup,你可以很容易地摆脱整个"非此即彼"的事情。
这实际上可以解决您的问题:返回上一个TabActivity