Android 使用后退按钮在片段之间切换
好的,我有一个带有一个主片段的活动,上面有一个菜单。当用户单击菜单项时,屏幕上将显示另一个片段的动画,代码如下:Android 使用后退按钮在片段之间切换,android,android-fragments,Android,Android Fragments,好的,我有一个带有一个主片段的活动,上面有一个菜单。当用户单击菜单项时,屏幕上将显示另一个片段的动画,代码如下: FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out); ft.hide(getFragmentManager().findFragmentByTag("menu_fragme
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
ft.hide(getFragmentManager().findFragmentByTag("menu_fragment"));
Fragment opisFragment = getFragmentManager().findFragmentByTag("opis_fragment");
if (opisFragment == null) {
opisFragment = new OpisFragment();
ft.add(R.id.p_container, opisFragment, "opis_fragment");
ft.commit();
} else {
ft.show(opisFragment);
}
注意:pr_fragment是当前片段的标记,即具有菜单的片段
现在,这很好,但是当我在第二个片段上时,我想添加功能,当用户单击后退按钮时,它将显示第一个片段。使用此代码,当我单击“上一步”时,它将一起退出活动。
谢谢你的帮助 您只需使用FragmentTransaction
// Showing menu fragment also added in backstack
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out)
.add(R.id.p_container, menuFragment, "menu_fragment")
.addToBackStack("menu_fragment")
.commit();
// Showing opis fragment also added in backstack
FragmentTransaction ft2 = getFragmentManager().beginTransaction();
ft2.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out)
.add(R.id.p_container, opisFragment, "opis_fragment")
.addToBackStack("opis_fragment")
.commit();
假设“opis片段”位于前台,当您按下后退按钮时,“菜单片段”将显示回前台,再次按下后退按钮将退出活动。您只需使用片段事务
// Showing menu fragment also added in backstack
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out)
.add(R.id.p_container, menuFragment, "menu_fragment")
.addToBackStack("menu_fragment")
.commit();
// Showing opis fragment also added in backstack
FragmentTransaction ft2 = getFragmentManager().beginTransaction();
ft2.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out)
.add(R.id.p_container, opisFragment, "opis_fragment")
.addToBackStack("opis_fragment")
.commit();
假设“opis片段”位于前台,当您按下后退按钮时,“菜单片段”将显示回前台,再次按下后退按钮将退出活动。
使用此代码,当我单击后退时,它将一起退出活动。
正常,因为应用程序堆栈中只有您的活动。addToBackStack()
方法就是您要寻找的方法
if (opisFragment == null) {
opisFragment = new OpisFragment();
ft.add(R.id.p_container, opisFragment, "opis_fragment");
ft.addToBackStack("tag"); // <<< this line
ft.commit();
}
if(opisFragment==null){
opisFragment=新的opisFragment();
ft.add(R.id.p_容器,opisFragment,“opis_片段”);
ft.addToBackStack(“tag”);//使用此代码,当我单击后退时,它将一起退出活动。
正常,因为你的应用程序堆栈中只有你的活动。addToBackStack()
方法就是你要找的
if (opisFragment == null) {
opisFragment = new OpisFragment();
ft.add(R.id.p_container, opisFragment, "opis_fragment");
ft.addToBackStack("tag"); // <<< this line
ft.commit();
}
if(opisFragment==null){
opisFragment=新的opisFragment();
ft.add(R.id.p_容器,opisFragment,“opis_片段”);
ft.addToBackStack(“tag”);//在mainactivity中,您可以检查碎片数,如果碎片数超过一个,我们将显示后退按钮
if(getSupportFragmentManager().getBackStackEntryCount() > 0)
{
mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
else
{
mDrawerToggle.setDrawerIndicatorEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
在mainactivity中,您可以检查碎片数,如果碎片数超过一个,我们将显示后退按钮
if(getSupportFragmentManager().getBackStackEntryCount() > 0)
{
mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
else
{
mDrawerToggle.setDrawerIndicatorEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
哦,哇,这非常简单。还有一件事,当我从opis_片段转到菜单_片段(使用后退按钮)时,如何添加动画?setCustomAnimations(int-enter,int-exit,int-popEnter,int-popExit)FragmentTransaction
的
将完成这项工作。请看这个哦,哇,这非常简单。还有一件事,当我从opis\u片段转到menu\u片段(使用后退按钮)时,如何添加动画?设置自定义动画(int-enter、int-exit、int-pocenter、int-popExit)FragmentTransaction的
将完成此工作。另请参见此