gpt4 book ai didi

Android Honeycomb : How to change Fragments in a FrameLayout, 没有重新创建它们?

转载 作者:可可西里 更新时间:2023-11-01 19:06:33 25 4
gpt4 key购买 nike

是否可以在 Fragment 之间切换而无需一直重新创建它们?如果是,怎么办?

In the documentation我找到了一个如何替换 fragment 的例子。

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

但我不想每次需要时都从头开始创建我的 fragment 。

我还找到了this example隐藏/显示 fragment :

// The content view embeds two fragments; now retrieve them and attach
// their "hide" button.
FragmentManager fm = getFragmentManager();
addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));

但是我如何在 XML 文件之外创建一个带有 ID 的 fragment 呢?

我认为这可能与 this question 有关, 但没有答案。 :/

非常感谢您,水母

编辑:

我现在就是这样做的:

Fragment shown = fragmentManager.findFragmentByTag(shownFragment);

//...

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (shown != null) fragmentTransaction.hide(shown);

//switch statetement for menu selection, just one example:

SettingsFragment set = (SettingsFragment) fragmentManager.findFragmentByTag(SET);
Toast.makeText(this, "Settings:" + set, Toast.LENGTH_LONG).show();
if (set == null)
{
set = new SettingsFragment();
fragmentTransaction.add(R.id.framelayout_content, set, SET);
}
else fragmentTransaction.show(set);
shownFragment = SET;
fragmentTransaction.commit();

如果我调出设置,然后调出其他东西,然后返回设置, toast 首先给我“null”,然后是“Settings:SettingsFragment{40ef...”。

但是,如果我将 fragmentTransaction.add(R.id.framelayout_content, set, SET); 替换为 fragmentTransaction.replace(R.id.framelayout_content, set, SET); 我一直收到“null”、“null”、“null”...所以它似乎没有通过标签找到 fragment 。

编辑2:

添加 fragmentTransaction.addToBackStack(null); 成功了。 :)这节省了整个隐藏/内存显示哪个 fragment 的部分,所以我认为这是最优雅的解决方案。

我找到了 this教程对该主题很有帮助。

编辑3:

查看我的代码,我意识到我可以去掉一些部分,所以我将其更改为:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (shown != null) fragmentTransaction.hide(shown);
Settings set = (Settings) fragmentManager.findFragmentByTag(SET);
if (set == null) set = new Settings();

fragmentTransaction.replace(R.id.framelayout_content, set, SET);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

但是,这调用了一个IllegalStateException: Fragment already added,与here 非常相似.有没有简单的方法来防止这种情况?否则我想我可能会切换回隐藏/显示位。

最佳答案

这可能取决于您试图避免重新创建的内容。

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

在您的示例中,当您从 newFragment 中点击后退按钮时,将显示前一个 fragment (您将获得一个 onCreateView、onActivityCreated 但没有 onCreate),因此该 fragment 不会被重新创建。至于你的 newFragment,如果你打算再次使用它来更新任何内部状态,你仍然可以保留它,比如 onCreate 或 onActivityCreated。

编辑:

如果您只是有一个菜单列表,每个条目在右 Pane 中调用不同的 fragment ,那么添加到返回堆栈并不是您想要的。为此,您可以预先在每个 fragment 上调用 ​​add(...) 并根据需要简单地隐藏/显示每个 fragment (我没有对此进行测试)。否则我会建议保留对每个 fragment 的引用,在选择不同的菜单项时调用 replace(...) 确保您不会添加到返回堆栈。

关于Android Honeycomb : How to change Fragments in a FrameLayout, 没有重新创建它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6185272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com