我遇到了SuportMap片段的问题,当我切换回Map片段时它显示一个空白片段,这就是这种情况: 我有左抽屉的主要活动,抽屉包含两个项目以显示片段A和片段B,片段A包含标签主机和2个标签(地图标签和第二个标签)。 应用程序在午餐时显示Map,没关系,但如果我选择抽屉中的第二个项目转到片段B,然后回到片段A,则Map片段显示一个空白片段,如果我选择另一个选项卡并返回到地图选项卡,然后再次成功显示地图。我不知道有什么不对。
MainActivity:
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
MainFragment mainFragment = (MainFragment) getSupportFragmentManager().findFragmentByTag(MainFragment.TAG);
if (id == R.id.nav_home && mainFragment == null) {
showFragment(new MapFragment(), "MapFragment");
} else if (id == R.id.nav_gallery &&
getSupportFragmentManager().findFragmentByTag(GalleryFragment.TAG) == null) {
showFragment(new B(), "B");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
assert drawer != null;
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void showFragment(Fragment fragment, String tag) {
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate(tag, 0);
if (!fragmentPopped) { //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content, fragment);
ft.addToBackStack(tag);
ft.commit();
}
}
片段A:
private void initTabHost() {
mTHost.setOnTabChangedListener(this);
mTHost.setup();
TabHost.TabSpec MapSpec = mTHost.newTabSpec(PeopleFragment.TAG);
MapSpec.setIndicator(getTabIndicator(R.string.tool_bar_people, R.drawable.selector_map_tab));
MapSpec.setContent(new DummyTabContent(getActivity()));
mTHost.addTab(MapSpec);
TabHost.TabSpec secondSpec = mTHost.newTabSpec(SecondFragment.TAG);
secondSpec.setIndicator(getTabIndicator(R.string.tool_bar_news, R.drawable.selector_second_tab));
secondSpec.setContent(new DummyTabContent(getActivity()));
mTHost.addTab(secondSpec);
}
private View getTabIndicator(int title, int icon) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_tab_layout, null);
ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_icon);
ivIcon.setImageResource(icon);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
tvTitle.setText(title);
return view;
}
@Override
public void onTabChanged(String tabId) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MapFragment mapFragment = (MapFragment) fragmentManager.findFragmentByTag(MapFragment.TAG);
SecondFragment secondFragment = (SecondFragment) fragmentManager.findFragmentByTag(SecondFragment.TAG);
if (mapFragment != null) {
fragmentTransaction.detach(mapFragment);
}
if (secondFragment != null) {
fragmentTransaction.detach(secondFragment);
}
if (tabId.equalsIgnoreCase(MapFragment.TAG)) {
if (mapFragment == null) {
fragmentTransaction.add(R.id.fl_real_tab_content, new MapFragment(), MapFragment.TAG);
} else {
fragmentTransaction.attach(mapFragment);
}
} else if (tabId.equalsIgnoreCase(SecondFragment.TAG)) {
if (secondFragment == null) {
fragmentTransaction.add(R.id.fl_real_tab_content, new SecondFragment(), SecondFragment.TAG);
} else {
fragmentTransaction.attach(SecondFragment);
}
}
fragmentTransaction.commit();
}
MapFragment:
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
public void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the mMap.
((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
.getMapAsync(this);
}
任何帮助我该怎么办?问题是什么?
答案 0 :(得分:0)
我已在解决方案中修复了此问题,方法是将android:launchMode=="SingleInstance"
添加到清单文件中的活动中。
在这种情况下,当我们返回活动时,活动将不会创建新的碎片并从最初重置状态。