我有三个片段:
A是我的主要片段,B是登录片段,成功登录将进入C片段。
当我点击后退按钮时,我需要从片段C移动到片段A。
我的问题是,当我点击后退按钮时,我仍然会从片段A移动片段B.
如何解决我的问题?
这是我的切换片段功能:
public void switchFragment(Fragment fragment) {
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.mainFrame, fragment, null);
transaction.addToBackStack(null);
transaction.commit();
}
片段(NewHomepage)到B(LoginFragment)片段:
switchFragment(LogInFragment.newInstance());
这是我的B片段,当它来自C片段时,它具有值logged
来决定是否切换A片段。
我认为问题必须在这里,当回到A片段并单击后退按钮想要退出APP时,我可以看到logcat show 1 =>和2 =>
String logged = memberData.getUD_MBTYPENAME(); //get the value when login succeed
Log.d(TAG,"1=>"+logged);
//If UD_MBTYPENAME is not null,change to A fragment
if (!TextUtils.isEmpty(logged)) {
Log.d(TAG,"2=>"+logged);
((MainActivity) getActivity()).switchFragment(NewHomepage.newInstance());
}
这是关于onKeyDown和switchFragment的我的MainActivity:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
quickDialog();//It's a alert dialog
return false;
}
}
return super.onKeyDown(keyCode, event);
}
public void switchFragment(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.mainFrame, fragment, null);
transaction.addToBackStack(null);
transaction.commit();
}
参考更改此类代码,如果我不使用transaction.addToBackStack(null);
,问题仍然存在,即使删除transaction.addToBackStack(null);
当我回到A时,我必须再次点击两次以显示警告对话框,我不知道当我第一次点击A片段时发生了什么。
if (!TextUtils.isEmpty(logged)){
Log.d(TAG,"2=>"+logged);
hideFragment(LogInFragment.newInstance());
switchFragment(NewHomepage.newInstance());
}
public void switchFragment(Fragment fragment) {
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.mainFrame, fragment, null);
//remove it will fix my issue , but I have to click back twice to show alert dialog , I don't know what happened click it first time in A fragment.
//transaction.addToBackStack(null);
transaction.commit();
}
public void hideFragment(Fragment fragment) {
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.hide(fragment);
transaction.commit();
}
删除hideFragment
并在switchFragment上使用manager.popBackStack();
,问题将得到解决。
答案 0 :(得分:1)
尝试
如果已经添加片段,它将显示片段。
使用fragmentTransaction.show
方法重新使用现有片段,即保存的实例。
public void switchFragment (Fragment oldFragment, Fragment newFragment, int frameId) {
boolean addFragment = true;
FragmentManager fragmentManager = getFragmentManager ();
String tag = newFragment.getArguments ().getString ("TAG");
Fragment fragment = fragmentManager.findFragmentByTag (tag);
// Check if fragment is already added
if (fragment != null && fragment.isAdded ()) {
addFragment = false;
}
// Hide previous fragment
String oldFragmentTag = oldFragment.getArguments ().getString (BaseFragment.TAG);
if (!tag.equals (oldFragmentTag)) {
FragmentTransaction hideTransaction = fragmentManager.beginTransaction ();
Fragment fragment1 = fragmentManager.findFragmentByTag (oldFragmentTag);
hideTransaction.hide (fragment1);
hideTransaction.commit ();
}
// Add new fragment and show it
FragmentTransaction addTransaction = fragmentManager.beginTransaction ();
if (addFragment) {
addTransaction.add (frameId, newFragment, tag);
addTransaction.addToBackStack (tag);
}
else {
newFragment = fragmentManager.findFragmentByTag (tag);
}
addTransaction.show (newFragment);
addTransaction.commit ();
}