如何避免将相同的片段添加到堆栈 [英] How to avoid adding same fragment to stack
问题描述
我需要一些帮助. em通过这种方式向活动添加片段.问题是在openFragment的每个调用上它都会创建片段并添加.这是显而易见的.问题:我做了什么修改,因此它只能添加一次片段.在下一个使用相同fragment标签的调用中,它将什么都不做.
I need some help. em adding fragment to activity this way. problem is on each call of openFragment it create fragment and add. which is obvious. Question: what modification i do, so it can add fragment only once. on the next call with same fragment tag it will do nothing.
case:第一次按下按钮添加片段并显示.我再次按下相同的按钮,它什么也没反应.
case: press button first time it add fragment and shows. i press again same button it response nothing.
public static void openFragment(Activity activity, Fragment fragment) {
FragmentManager fragmentManager = ((ActionBarActivity) activity)
.getSupportFragmentManager();
FragmentTransaction ftx = fragmentManager.beginTransaction();
ftx.addToBackStack(fragment.getClass().getSimpleName());
ftx.setCustomAnimations(R.anim.slide_in_right,
R.anim.slide_out_left, R.anim.slide_in_left,
R.anim.slide_out_right);
ftx.add(R.id.main_content, fragment);
ftx.commit();
}
推荐答案
这里是解决方案,它将只允许您在堆栈中添加一次片段,否则将从堆栈中弹出完全相同的片段.
Here's the solution, It will only allow you to add fragment once in stack otherwise it will pop-out the very same fragment from stack.
public static void openFragment(Activity activity, Fragment fragment) {
String fragmentTag = fragment.getClass().getName();
FragmentManager fragmentManager= ((ActionBarActivity) activity)
.getSupportFragmentManager();
boolean fragmentPopped = fragmentManager
.popBackStackImmediate(fragmentTag , 0);
if (!fragmentPopped && fragmentManager.findFragmentByTag(fragmentTag) == null) {
FragmentTransaction ftx = fragmentManager.beginTransaction();
ftx.addToBackStack(fragment.getClass().getSimpleName());
ftx.setCustomAnimations(R.anim.slide_in_right,
R.anim.slide_out_left, R.anim.slide_in_left,
R.anim.slide_out_right);
ftx.add(R.id.main_content, fragment);
ftx.commit();
}
}
slide_in_right
slide_in_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate android:fromXDelta="100%"
android:toXDelta="0%" android:fromYDelta="0%"
android:toYDelta="0%" android:duration="200">
</translate>
</set>
slide_out_right
slide_out_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="200">
</translate>
</set>
slide_in_left
slide_in_left
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate android:fromXDelta="-100%"
android:toXDelta="0%" android:fromYDelta="0%"
android:toYDelta="0%" android:duration="200">
</translate>
</set>
slide_out_left
slide_out_left
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate android:fromXDelta="0%"
android:toXDelta="-100%" android:fromYDelta="0%"
android:toYDelta="0%" android:duration="200">
</translate>
</set>
这就是您调用此函数的方式:
And this is how you call this function:
openFragment(activity, new MyFragment());
这篇关于如何避免将相同的片段添加到堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!