gpt4 book ai didi

android - fragment 未添加到后台堆栈

转载 作者:行者123 更新时间:2023-11-29 17:52:37 25 4
gpt4 key购买 nike

我在让 Fragments 和 backstack 正常工作时遇到了问题。

我有一个带有 FrameLayout 的布局,用于保存各种 fragment 和另一个 fragment :

<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/filterableListContainer"
android:layout_weight="50">
</FrameLayout>


<fragment class="com.facetoe.remotempd.fragments.PlayerBarFragment"
android:id="@+id/playerBarFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:layout="@layout/player_bar"/>
</LinearLayout>

当我启动使用此布局的 Activity 时,我将 fragment 添加到 FrameLayout,如下所示:

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
if (findViewById(R.id.filterableListContainer) != null) {

// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}

// Create a new Fragment to be placed in the activity layout
ArtistListFragment listFragment = new ArtistListFragment();

// Add the fragment to the 'fragment_container' FrameLayout
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.filterableListContainer, listFragment);
ft.addToBackStack(null);
ft.commit();
} else {
Log.e(TAG, "Couldn't find filterableListFragment");
}

}

当用户点击一个项目时,我尝试用这段代码替换 fragment :

            ArtistAlbumsListFragment fragment = new ArtistAlbumsListFragment(getActivity(), albums);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.filterableListContainer, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();

但是,当我按下后退按钮时,我会返回到主屏幕。谁能告诉我哪里出错了?

最佳答案

好的,我修好了。当我在 Intellij 中创建 Activity 时,它创建了 ActionBarActivity 的子类。删除 ActionBarActivity 并将其替换为 Activity 使 fragment 转换按预期工作。

关于android - fragment 未添加到后台堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21950060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com