gpt4 book ai didi

java - 切换Activity后Fragment Activity消失

转载 作者:行者123 更新时间:2023-12-02 05:15:23 25 4
gpt4 key购买 nike

我的抽屉导航有 6 个 fragment ,其中一个包含另外 2 个用于 viewPager 的 fragment 。我的问题是,当我从包含 viewPager(HomeFragment) 的 fragment 切换到另一个 fragment 并切换回来时,HomeFragment 中两个 fragment 的内容都会消失。

这是我的代码

MainActivity.class

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

...
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
navigationView.setCheckedItem(R.id.nav_home);
...
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {

case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
toolbar.setTitle("mSeries");
toolbar.setTitleTextColor(getResources().getColor(R.color.colorPrimaryDark));
break;
...

HomeFragment.class

public class HomeFragment extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

ViewPager viewPager = view.findViewById(R.id.viewpager);
setupViewPager(viewPager);

TabLayout tabLayout = view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {

ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new UpcomingShowsActivity(), "Upcoming episodes");
adapter.addFragment(new UnwatchedEpisodesActivity(), "Unwatched episodes");
viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@NonNull
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

UpcomingShowsActivity.class

public class UpcomingShowsActivity extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_upcoming_episodes, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}

我是android新手,如果有更好或更有效的方法来实现这一点,请告诉我。

感谢您的宝贵时间。

最佳答案

我自己修复了这个问题,并发布此内容以防将来有人遇到类似问题

因为我要在另一个 fragment 中创建 fragment ,所以我应该在 HomeFragment.class 中使用 getChildFragmentManager 而不是 get getFragmentManager

private void setupViewPager(ViewPager viewPager) {

ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new UpcomingShowsActivity(), "Upcoming episodes");
adapter.addFragment(new UnwatchedEpisodesActivity(), "Unwatched episodes");
viewPager.setAdapter(adapter);
}

关于java - 切换Activity后Fragment Activity消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56298992/

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