我想知道为什么当我切换它们时我的片段留在后台。
以下是要解释的图片: https://imgur.com/LnvcDct
这个问题似乎非常罕见,它正在谈论后台堆栈。你能解释一下吗?
所以,这是我的代码:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public FragmentTransaction fragmentTransaction;
public FragmentManager fragmentManager = getFragmentManager();
public Fragment FragmentToDisplay = null;
public static Context cMainActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
cMainActivity = this;
displaySelectedScreen(R.id.main_layout);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
displaySelectedScreen(item.getItemId());
return true;
}
private void displaySelectedScreen(int ItemId){
if (ItemId == R.id.main_layout){
FragmentToDisplay = new homeFragment();
} else if (ItemId == R.id.led_menu) {
FragmentToDisplay = new ledFragment();
} else if (ItemId == R.id.pin_menu) {
} else if (ItemId == R.id.ecran_menu) {
}
if (FragmentToDisplay != null) {
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentLayout, FragmentToDisplay);
fragmentTransaction.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
Log.i("Display something", "Display Home");
}}
这里是我片段的代码:
public class ledFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.led_layout, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}}
提前致谢!
答案 0 :(得分:0)
替换:
fragmentTransaction.add(R.id.fragmentLayout, FragmentToDisplay);
使用:
fragmentTransaction.replace(R.id.fragmentLayout, FragmentToDisplay);
答案 1 :(得分:0)
查看Fragments Transactions文档,您可以更好地了解add()
和replace()
答案 2 :(得分:0)
此方法将片段添加到片段中,剩下的片段继续工作。如果您不想这样做。
if (FragmentToDisplay != null) {
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentLayout, FragmentToDisplay);
fragmentTransaction.commit();
}
尝试这种方法
{{1}}