- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我使用了一个 Activity 和两个 fragment 。该应用程序使用带有容器的布局,因此 fragment 是通过事务添加的。第一个 fragment 包含一个 ListView ,另一个 fragment 包含 ListView 项的详细 View 。两个 fragment 都使用 setRetainInstance(true)。通过替换事务添加 fragment 并设置 addToBackStack(null)。 listfragment 包含一个实例变量,其中包含列表的一些信息。现在我要更改为详细信息并按回,实例变量为空。我阅读了 setRetainInstance 和 addToBackStack 并删除了 addToBackStack,但即便如此,实例变量仍为空。
有谁知道我可能做错了什么?
问候,托马斯
最佳答案
setRetainInstance(true)
将告诉 FragmentManager
在包含的 Activity
因某种原因被终止和重建时保留 fragment 。它不保证 Fragment
实例会在添加或替换事务后继续存在。听起来您的适配器正在被垃圾回收,而您并没有创建新适配器。
一个更简单的解决方案是制作一个无 View 的 Fragment
来保留您的 ListAdapter
。您这样做的方法是创建 Fragment
,将 retain 实例设置为 true,并在 onCreateView()
方法中返回 null
。要添加它,只需通过 FragmentTransaction
调用 addFragment(Fragment, String)
。您永远不会删除或替换它,因此它会在应用程序运行期间始终保留在内存中。屏幕旋转不会杀死它。
每当创建 ListFragment
时,在 onCreateView()
中获取 FragmentManager
并使用方法 findFragmentById()
或 FindFragmentByTag()
从内存中检索您保留的 fragment 。然后从该 fragment 中获取适配器并将其设置为列表的适配器。
public class ViewlessFragment extends Fragment {
public final static string TAG = "ViewlessFragment";
private ListAdapter mAdapter;
@Override
public ViewlessFragment() {
mAdapter = createAdater();
setRetainInstance(true);
}
@Override
public void onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return null;
}
public ListAdapter getAdapter() {
return mAdapter;
}
}
public class MyListFragment extends ListFragment {
final public static String TAG = "MyListFragment";
@Override
public void onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View returnView = getMyView();
final ViewlessFragment adapterFragment = (ViewlessFragment) getFragmentManager().findFragmentByTag(ViewlessFragment.TAG);
setListAdapter(ViewlessFragment.getAdapter());
return returnView;
}
}
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle icicle) {
// ... setup code...
final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
ViewlessFragment adapterFragment = fm.findFragmentByTag(ViewlessFragment.TAG);
if(adapterFragment == null) {
ft.add(new ViewlessFragment(), ViewlessFragment.TAG);
}
ft.add(R.id.fragmentContainer, new MyListFragment(), MyListFragment.TAG);
ft.commit();
}
}
关于android - 一个 Activity,多个 Fragments 和 setRetainInstance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14380234/
我正在尝试使用 setRetainInstance(),但它似乎对我不起作用! =(。我将问题简化为最简单的版本: 我有一个带有 TextView 和 Button 的 fragment 。 Text
我正在使用无 View fragment 在我的 Activity 方向更改期间存储一些数据。它看起来大致是这样的: public class BoardActivity extends BaseAc
我有一个 DialogFragment 附加到 Activity,当屏幕旋转时,Activity 被销毁,DialogFragment 也被销毁,我想保留 DialogFragment 和我的Dial
假设我有一个 fragment A。它有一个实例变量 mViewPager 指向它的 ViewPager。 在 Fragment A 的 onCreate(Bundle) 中,我调用了 setReta
当您在 Fragment 上调用 setRetainInstance(true) 时,究竟会发生什么?文档几乎不存在,这似乎是一个非常重要的功能。具体来说,我想知道这个序列(我编造的)有多少是真的:
我正在使用一个具有 SearchActivity 的应用程序;搜索 Activity 只是“SearchFragment”的容器,“SearchFragment”在ActionBar中包含一个Sear
这让我受够了。无论我如何尝试,我都无法在设备旋转时保留 textview 的状态。 示例如下: activity_main.xml RotationFragmentDemo.
我用的是ListFragment 设置保留实例(真);不工作,我不知道为什么 savedInstanceState == null 我的资源 public class Fragment_Left
如果我有一个 Activity 承载一个带有 setRetainInstance(true) 的 Fragment 如果这个 Activity 在后台被杀死会发生什么,即另一个 Activity 在它
对所有 Fragment 使用 Fragment.setRetainInstance() 以摆脱处理 Fragment 是一种好习惯吗娱乐、保存实例状态等?为什么不呢? 最佳答案 是的,如果它们必须保
我试图在方向更改时保存一个 ListFragment 子类,因此我将 setRetainInstance(true) 添加到我的 onCreate 函数的末尾。我添加了一个 onSaveInstanc
背景 我有一个 fragment 的 Activity ,在创建时需要动画,但在方向改变时不需要。 fragment 被动态插入到布局中,因为它是抽屉导航式 Activity 的一部分。 问题 我想避
Here , Dianne 说通过 onRetainNonConfigurationInstance() 保留对象的旧方法现在已经过时了,因为您可以在配置更改时保留 Fragment 实例。 还有 h
我发现 Fragment#setRetainInstance(true) 令人困惑。这是从 Android Developer API 中提取的 Javadoc : public void setRe
我创建了一个包含4个片段的底部导航项目,并将setHasOptionsMenu(true)放在qponFragment的onCreate()中,以便在切换Fragments之后保持qponFragme
从文档中得知,setRetainInstance(boolean) 方法有助于在设备配置更改时保留所有 Activity 对象,但我阅读的大多数示例和文档都涉及 headless fragment 。
我通过替换在多个 fragment 之间切换。 fragment 也被添加到返回堆栈。 现在我希望在方向改变时不再创建 fragment 。我将 setRetainInstance(true) 放在它
我正在学习 Google 提供的高级 android 开发培训。这是 link .我遇到了这个:Added a static fragment in xml 并且在旋转设备时,所选单选按钮的状态将保留
我有一个包含单个子 fragment 的父 fragment , fragment 的 onAttach 和 onCreateView 方法都被记录下来用于调试目的。 当我以这种方式将父 fragme
如果我在这方面有任何错误,请纠正我。这是一个澄清问题,因为我没有看到它在任何地方明确写过。 在 Android 4 中,您可以在 Fragment 上调用 setRetainInstance(true
我是一名优秀的程序员,十分优秀!