[英]Don't keep Activity in cache with TabLayout
這是我的應用程序結構:我有一個帶有4個標簽的主活動(帳戶,儀表板,媒體,應用程序)。 每個選項卡都鏈接到一個活動。 “應用程序”選項卡包含許多活動,為了使這些選項卡可見,我在“ 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,您可以輕松擺脫整個if-else事情。
這實際上可以解決您的問題: 返回上一個TabActivity
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.