在我的应用程序中,我使用了一个活动的片段。我创建了一个方法,它将调用新的片段。该方法工作正常,但是当我按下后退按钮我的应用程序关闭,即使我已经在后面放置片段stack.Please帮帮我。
代码
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
try {
String backStateName = fragment.getClass().getName();
String fragmentTag = backStateName;
FragmentManager manager = getFragmentManager();
// boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
if (manager.findFragmentByTag(fragmentTag) == null) {
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.frag_container, fragment, fragmentTag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
if (addToBackStack) {
ft.addToBackStack(backStateName);
} else {
ft.addToBackStack(null);
}
ft.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
使用此:
EditProfileFragment mEditProfileFragment = new EditProfileFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.content_frame,
mEditProfileFragment);
fragmentTransaction.addToBackStack(MyProfileFragment.class
.getName());
fragmentTransaction.commit();
答案 1 :(得分:0)
您是否正在处理活动?如下代码:
@Override
public void onBackPressed()
{
FragmentManager fragmentManager = getSupportFragmentManager();
int count = fragmentManager.getBackStackEntryCount();
if (1 < count)
{
fragmentManager.popBackStackImmediate();
}else
{
super.onBackPressed();
}
}