我当前的Android应用程序有两个片段: -
ListFragment
Detailfragment
我的布局XML类似于: -
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_anchorGravity="bottom"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>
我首先显示ListFragment,如下所示: -
final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(containerId, mListFragment, LIST_FRAGMENT_TAG);
if (mLandscape) {
} else {
fragmentTransaction.addToBackStack(LIST_FRAGMENT_TAG);
}
fragmentTransaction.commit();
当用户点击List项时,我想隐藏List以便保持当前列表位置等..并显示detailFragment。
下面是用于执行此UI更改的代码: -
mDetailFragment = new DetailFragment();
final Bundle fragmentArguments = new Bundle();
fragmentArguments.putString(ITEM_KEY, item.getKey());
mDetailFragment.setArguments(fragmentArguments);
final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (mLandscape) {
fragmentTransaction.replace(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
} else {
fragmentTransaction.hide(mListFragment);
fragmentTransaction.add(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
}
fragmentTransaction.commit();
上面的代码都运行正常,我可以在List和Detail Fragments之间成功转换。
我遇到的问题是,当用户按下细节片段上的BACK BUTTON返回ListFragment时,它们会返回到空白屏幕。
我在我的Activity中有这个代码来删除详细信息片段并显示隐藏的List片段: -
@Override
public void onBackPressed() {
if (mLandscape) {
} else {
if (mListFragment.isHidden()) {
final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(mDetailFragment);
fragmentTransaction.show(mListFragment);
fragmentTransaction.commit();
}
}
super.onBackPressed();
}
为什么fragmentTransaction.show(mListFragment);
没有显示隐藏的ListFragment?
注意:因此我总是在方向更改上完全重建我的ui我已将空包传递给super.onCreate(savedInstanceStateNull);
private final Bundle savedInstanceStateNull = null;
private boolean mLandscape = false;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceStateNull);
setContentView(R.layout.activity_article_list);
mLandscape = getResources().getBoolean(R.bool.landscape);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
manageFragments();
}
答案 0 :(得分:1)
以下是我如何解决这个问题: -
首先删除我的覆盖onBackPressed()
将显示ListFragment更改为: -
final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(containerId, mListFragment, LIST_FRAGMENT_TAG);
fragmentTransaction.commit();
将显示detailFragment更改为: -
mDetailFragment = new DetailFragment();
final Bundle fragmentArguments = new Bundle();
fragmentArguments.putString(ITEM_KEY, item.getKey());
mDetailFragment.setArguments(fragmentArguments);
final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (mLandscape) {
fragmentTransaction.replace(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
} else {
fragmentTransaction.hide(mListFragment);
fragmentTransaction.add(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
fragmentTransaction.addToBackStack(DETAIL_FRAGMENT_TAG);
}
fragmentTransaction.commit();