我正在使用MainActivity来管理多个片段的显示。我有一个showFragment方法,它迭代我的片段数组并显示/隐藏它们,并相应地设置backstack。问题在于,无论何时按下后退按钮,都会显示HomeFrag,其中所有其他碎片都堆积在下面。我在文档中的印象是后退按钮将反转最后一个事务(在提交之前添加的任何内容)。任何人都可以解释为什么这可能会导致这种意外行为?这是我的主要活动。这些片段都在activity_main.xml中定义:
public class MainActivity extends MenuBaseActivity {
private static final int HOME = 0;
private static final int WRITE = 1;
private static final int UPLOAD = 2;
private static final int SHARE = 3;
private static final int SNAP = 4;
private static final int POSITION = 5;
private static final int FRAGMENT_COUNT = POSITION + 1;
/** The current fragment being displayed **/
private int mCurrentFragment = -1;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
fragments[HOME] = fm.findFragmentById(R.id.homeFrag);
fragments[WRITE] = fm.findFragmentById(R.id.writeFrag);
fragments[UPLOAD] = fm.findFragmentById(R.id.uploadFrag);
fragments[SHARE] = fm.findFragmentById(R.id.shareFrag);
fragments[SNAP] = fm.findFragmentById(R.id.snapFrag);
fragments[POSITION] = fm.findFragmentById(R.id.positionFrag);
// If no fragments are currently being displayed, show the home frag
if(mCurrentFragment == -1) {
showFragment(0,true);
}
}
/*
* Handles the click event for all navigation buttons (navOnClick) in the fragments.
*/
//TODO: Tag navigation with text strings instead of integers
public void navOnClick(View v) {
Object objectTag = v.getTag();
int tag = Integer.valueOf(objectTag.toString());
showFragment(tag, true);
}
public void showFragment(int fragmentIndex, boolean addToBackStack) {
Log.v(Constants.getLogTag(), "Main: fragmentIndex:" + fragmentIndex + " backstack:" + addToBackStack);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
//ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
ft.show(fragments[i]);
} else {
ft.hide(fragments[i]);
}
}
if (addToBackStack) {
Log.v(Constants.getLogTag(), "Main: Adding to backstack");
ft.addToBackStack(null);
}
//Update the current fragment identifier
mCurrentFragment = fragmentIndex;
ft.commit();
}
}
答案 0 :(得分:1)
我个人没有对此进行过测试,但请告诉我这是如何运作的。
我实际上决定保留数组,这样我们就可以在首次启动活动时正确隐藏每个片段。我改变了一些应该解决问题的小事。每次循环都不需要隐藏每个片段。您每次只需隐藏当前片段。
public class MainActivity extends MenuBaseActivity {
private static final int HOME = 0;
private static final int WRITE = 1;
private static final int UPLOAD = 2;
private static final int SHARE = 3;
private static final int SNAP = 4;
private static final int POSITION = 5;
/** The current fragment being displayed **/
private int mCurrentFragment = HOME;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
fragments[HOME] = fm.findFragmentById(R.id.homeFrag);
fragments[WRITE] = fm.findFragmentById(R.id.writeFrag);
fragments[UPLOAD] = fm.findFragmentById(R.id.uploadFrag);
fragments[SHARE] = fm.findFragmentById(R.id.shareFrag);
fragments[SNAP] = fm.findFragmentById(R.id.snapFrag);
fragments[POSITION] = fm.findFragmentById(R.id.positionFrag);
// Hide all of the fragments except for the home fragment
for (int i = WRITE; i < fragments.length; i++) {
ft.hide(fragments[i]);
}
ft.commit();
}
/*
* Handles the click event for all navigation buttons (navOnClick) in the fragments.
*/
//TODO: Tag navigation with text strings instead of integers
public void navOnClick(View v) {
Object objectTag = v.getTag();
int tag = Integer.valueOf(objectTag.toString());
showFragment(tag, true);
}
public void showFragment(int fragmentIndex, boolean addToBackStack) {
Log.v(Constants.getLogTag(), "Main: fragmentIndex:" + fragmentIndex + " backstack:" + addToBackStack);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// I changed this part too. You will probably want to implement it this way
// if you want to also see an animation when hitting the back button. I will
// leave it up to you as an exercise to figure out how the other animator
// files should be constructed
ft.setCustomAnimations(R.animator.slide_in_right, R.animator.slide_out_left,
R.animator.slide_in_left, R.animator.slide_out_right);
ft.hide(fragments[mCurrentFragment]);
ft.show(fragments[fragmentIndex]);
if (addToBackStack) {
Log.v(Constants.getLogTag(), "Main: Adding to backstack");
ft.addToBackStack(null);
}
//Update the current fragment identifier
mCurrentFragment = fragmentIndex;
ft.commit();
}
}