0
我想在actionBar
中實現Home/Up按鈕。我有一個Activity(A)和兩個片段(設置(B)和About(C))的簡單應用。ActionBar主頁按鈕 - 隱藏片段並清除回棧
在其他問題我發現使用popBackStack
,但這不是解決方案,因爲如果用戶進入A> B> C> B> C,後退按鈕轉到C> B> C> B> A(這是正確的行爲?)。
switch (item.getItemId()){
case R.id.action_settings:
// get preferences fragment
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new PrefFragment())
.addToBackStack(null)
.commit();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
return true;
case R.id.action_about:
// get about fragment
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new AboutFragment())
.addToBackStack(null)
.commit();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
return true;
case android.R.id.home:
// clear back stack and show home screen?
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
return true;
}
我需要最後一種情況下隱藏的任何片段(表明主要活動窗口),並清除我的背部棧 - 回家,不回來了。我怎麼能做到這一點?
我使用AppCompat庫。
謝謝,我不得不使用'getFragmentManager'而不是'getSupportFragmentManager',否則我的後臺計數爲零。 – xYann00