gpt4 book ai didi

java - addToBackStack() 不适用于 getChildFragmentManager()

转载 作者:行者123 更新时间:2023-12-01 18:11:14 25 4
gpt4 key购买 nike

按后退按钮后,单击按钮时显示的新 fragment 不会关闭,而是关闭整个应用程序。该按钮位于三个选项卡之一内。 mainActivity.java、HeroesFragment(具有三个选项卡的 fragment )、StrengthHeroesFragment(具有按钮的三个选项卡之一)的代码如下。

MainActivity.java

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;


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

Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

drawer=findViewById(R.id.draw_layout);
NavigationView navigationView=findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawer,toolbar,
R.string.navigation_drawer_open,R.string.navigation_drawer_close);

drawer.addDrawerListener(toggle);
toggle.syncState();

if (savedInstanceState==null){
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new HeroesFragment()).commit();

navigationView.setCheckedItem(R.id.nav_heroes);}

}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_heroes:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new HeroesFragment()).commit();
break;
case R.id.nav_items:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new ItemsFragment()).commit();
break;
case R.id.nav_counterpicker:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new CounterPickerFragment()).commit();
break;
case R.id.nav_news:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new NewsFragment()).commit();
break;

}

drawer.closeDrawer(GravityCompat.START);
return true;
}

@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}
else{
super.onBackPressed();
}


}

}

HeroesFragment.java(包含三个选项卡的 fragment )

public class HeroesFragment extends Fragment {


private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private ViewPager viewPager;
View view;
RelativeLayout relativeLayout;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_heroes,container,false);

relativeLayout=(RelativeLayout)view.findViewById(R.id.hero_layout);
tabLayout=(TabLayout)view.findViewById(R.id.heroes_tablayout);
appBarLayout=(AppBarLayout)view.findViewById(R.id.hero_appbar);
viewPager=(ViewPager)view.findViewById(R.id.heroes_viewpager);
ViewPageAdapter adapter=new ViewPageAdapter(getChildFragmentManager(),1);
adapter.AddFragment(new StrengthHeroesFragment(),"Strength" );
adapter.AddFragment(new AgilityHeroesFragment(),"Agility");
adapter.AddFragment(new IntelligenceHeroesFragment(),"Intelligence");
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()){
case 0:
Toast.makeText(getContext(),"Tab 1",Toast.LENGTH_SHORT).show();
break;

case 1:
Toast.makeText(getContext(),"Tab 2",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getContext(),"tab 3",Toast.LENGTH_SHORT).show();
break;
default:
break;
}

}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);

return view;


}



}

StrengthHeroesFragment.java(包含按钮的 fragment )

public class StrengthHeroesFragment extends Fragment {

View view;
ImageButton ib1,ib2,ib3,ib4,ib5,ib6;
TextView t1,t2,t3,t4,t5,t6;


public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
Bundle savedInstanceState) {

view=inflater.inflate(R.layout.fragment_strength_heroes,container,false);


ib1=(ImageButton)view.findViewById(R.id.abaddon_icon);


ib1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HeroDetailsFragment heroDetailsFragment=new HeroDetailsFragment();

Bundle data=new Bundle();
data.putString("heroname","Abaddon");
heroDetailsFragment.setArguments(data);
FragmentTransaction transaction=getChildFragmentManager().beginTransaction();
transaction.replace(R.id.strength_hero_layout,heroDetailsFragment).addToBackStack(null);
transaction.commit();
}
});
return view;
}


}

当我在 StrengthHeroes fragment 中使用 getFragmentManager() 而不是 getChildFragmentManager() 时,它实际上可以工作,但是当我切换到抽屉导航中的另一个 fragment 时,选项卡布局 fragment 中的内容消失了。我还尝试使用 setPrimaryNavigationFragment() 它仍然不起作用。请帮我解决一下......提前致谢。

最佳答案

使用下面的代码代替 super.onBackpress()

@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}
else{
getSupportFragmentManager().popBackStack();

}
}

关于java - addToBackStack() 不适用于 getChildFragmentManager(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60466038/

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