gpt4 book ai didi

android-fragments - Android 阻止 webview 在片段中重新加载

转载 作者:行者123 更新时间:2023-12-02 03:30:16 24 4
gpt4 key购买 nike

我有一个包含框架布局和 4 个选项卡按钮的主要事件,总共有 4 个片段,每个片段与一个选项卡按钮相关,根据单击哪个选项卡按钮,相应的片段将交换到框架布局中更换旧的。现在在每个片段中都有一个 webview,它在创建片段时加载不同的 URL。我的问题是如何防止每次片段交换时 webview 重新加载?以下是我如何交换片段的代码:

FragmentMainPage fragment = new FragmentMainPage();
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.frameLayout, fragment);
transaction.commit();
currentPage = VT_Constants.HOME_PAGE;

最佳答案

调用 replace 是破坏性的:它会破坏片段的 View 层次结构,因此当您稍后将其添加回来时,必须重新构建 View ,这解释了您所看到的行为。

尝试显示和隐藏。它们维护片段的 View ,因此当用户单击该片段的选项卡时,它们可以重新附加到您的事件。像这样的东西(虽然我不确定你想如何获得对当前片段的引用):

FragmentMainPage fragment = new FragmentMainPage();
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
Fragment oldFragment = fm.findFragmentByTag(...); // somehow get the current fragment showing
FragmentTransaction transaction = fm.beginTransaction();
transaction.hide(oldFragment);
transaction.show(fragment);
transaction.commit();

关于android-fragments - Android 阻止 webview 在片段中重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27351298/

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