我目前正在開發一個應用程序,該應用程序由管理多個片段的基本活動組成。其中一個片段是一個列表,當點擊某個項目時將啓動一個詳細信息活動。我希望當我點擊主頁按鈕(使用ABS)作爲應用程序導航回到列表片段,而是總是導航回我指定爲默認片段。正確的從片段到活動的導航
Base Activity
Frag1 - Default
Frag2
Frag3 -> DetailsActivity
當從Home按鈕按下HomeActivity時,我想回到Frag3,而不是我去Frag1。
任何幫助將是偉大的。以下是我在詳細信息活動中處理正在按下的主頁按鈕的代碼。
主要活動:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the Above View
if (savedInstanceState != null)
mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
if (mContent == null)
mContent = new HomeFragment();
// set the Above View
setContentView(R.layout.content_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, mContent)
.commit();
// set the Behind View
setBehindContentView(R.layout.menu_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame, new SlidingMenuFragment())
.commit();
// customize the SlidingMenu
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}
@Override
public void onSaveInstanceState(Bundle outState) {
getSupportFragmentManager().putFragment(outState, "mContent", mContent);
super.onSaveInstanceState(outState);
}
public void switchContent(Fragment fragment) {
getSlidingMenu().showContent();
mContent = fragment;
getSupportFragmentManager()
.beginTransaction()
.addToBackStack("test")
.replace(R.id.content_frame, fragment)
.commit();
}
子活動:
@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
我認爲問題在於,當我返回主活動時,savedInstanceState始終爲空。我在上面添加了更多的代碼。 – Nath5