1

我的应用程序中有一些片段,但每次单击按钮时我的代码都会打开一个新片段。

我想知道如何更改这一点,并使片段返回到与我离开时完全相同的状态。

我现在使用的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragments);

        MainActivity fragment = new MainActivity();
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.add(R.id.fragment_place, fragment);
        transaction.commit();

        turnGPSOn();
    }

    public void onSelectFragment(View view) {

        if (view == findViewById(R.id.add)) 
        {
            newFragment = new Add();
        }
        else if (view == findViewById(R.id.map)) 
        {
            newFragment = new MainActivity();
        } 
        else 
        {
            newFragment = new Add();
        }

        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.replace(R.id.fragment_place, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

谢谢!

4

2 回答 2

1

你每次都得到一个新的片段,因为你每次都在调用new XXX()

我认为您可以使用findFragmentByTag它来解决这个问题。正如您在此处看到的,该replace函数可以接受第三个参数,即 String,该 String 可以用作 id 来标识您之前使用过的不同片段。

所以总结一下,你可以:

  1. 调用Fragment f = getSupportFragmentManager().findFragmentByTag("FragAdd");例如为了检索第一个片段。

  2. 如果 f 为空,则表示您尚未使用该片段,因此您必须调用new Add()如果没有,则使用该片段替换旧片段。例如像这样:

FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction();

transaction.replace(R.id.fragment_place, newFragment, "FragAdd"); //or whatever other string you want to use
transaction.addToBackStack(null);
transaction.commit();

希望能帮助到你 :)

于 2013-09-30T17:34:00.313 回答
1

我前段时间遇到过这个问题,并设法为一次只有一个可见片段的应用程序解决了这个问题;对于具有多个可见片段的活动,您需要进行一些调整。这就是我所做的。-

创建一个 custom ParentActivity,以便我的所有活动都扩展它。ParentActivity知道当前显示的是哪个片段,以及如何显示新片段。

public String currentFragmentTag;

public ParentFragment getCurrentFragment(int fragmentWrapperResId) { 
    ParentFragment res = null;  

    FragmentManager fragmentManager = getSupportFragmentManager();
    res = (ParentFragment) fragmentManager.findFragmentById(fragmentWrapperResId); 

    if (res != null && res.isHidden()) {
        if (currentFragmentTag != null) {
            res = (ParentFragment) fragmentManager.findFragmentByTag(currentFragmentTag);
        }
    }

    return res;
}

public void openFragment(ParentFragment fragment, int fragmentWrapperResId, int enterAnim, int exitAnim, int popEnterAnim, int popExitAnim, boolean addToBackStack) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    ParentFragment currentFragment = getCurrentFragment(fragmentWrapperResId);
    if (currentFragment != null && currentFragment.getTagName().equals(fragment.getTagName())) {
        return;
    }

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.setCustomAnimations(enterAnim, exitAnim, popEnterAnim, popExitAnim);

    if (currentFragment != null) {
        transaction.hide(currentFragment);
    }

    if (fragment.isAdded()) {
        transaction.show(fragment);
    } else {
        transaction.add(fragmentWrapperResId, fragment, fragment.getTagName()).setBreadCrumbShortTitle(fragment.getTagName());
    }

    if (addToBackStack) {
        transaction.addToBackStack(fragment.getTagName());
    } else {
        currentFragmentTag = fragment.getTagName();
    }

    transaction.commit();
}

创建一个ParentFragment, 由其余部分扩展Fragments, 带有一个标签 getter

public String getTagName() {
    return getClass().getSimpleName() + System.identityHashCode(this);
}

如您所见,主要思想不是替换可见片段,而是添加它们并在需要时显示/隐藏。这样,片段将保持其状态,因为在您将它们从bakstack中删除之前它们不会被销毁。

于 2013-09-30T17:35:51.723 回答