1

我真的,真的需要在这里得到认真的帮助。我正在创建一个PagerAdapter用于Fragmentsactivity. 不同的片段根据需要由运行时创建的不同视图组成。在最后一个片段中,我创建了一种,当单击它时,应该从所有片段"Submit button"的每个视图(如 )中获取用户输入的值。EditText

我正在使用以下命令来获取视图(在上面提到的按钮中clicklistener):

EditText e = (EditText) (getActivity().findViewById(i));

但除了最后两个片段之外,它没有获得具有该 ID 的视图。所以,我假设,它只保存了该活动中最后两个片段的状态。那么,如何保存'view states'活动中的所有片段?

不是这样吗,当在 Fragment 中创建视图并添加到其布局中时,该视图也添加到主活动布局中?我理解错了吗?请告诉我。

为了简化它,我的问题是:

我们如何将用户输入的内容保存在 Fragments 中动态创建的 EditText 中,使用 ViewPager 创建(以便以后可以访问)?

4

1 回答 1

0

When you add a fragment to a FragmentPagerAdapter (assuming you haven't written a custom pager) it is added to the FragmentManager. The Fragment manager is independent of the activity lifecycle so it retains the fragment state.

If you take a look here: http://code.google.com/p/ratebeerforandroid/source/browse/trunk/JakeWharton-ActionBarSherlock/library/src/android/support/v4/app/FragmentPagerAdapter.java?spec=svn42&r=42

You can get an idea of how it works. If you want to save the fragment state for another session you'll need to pull back each fragment (getItem) and either use the onSaveInstanceState or write your own custom function

Hope that helps

于 2011-10-07T22:16:27.280 回答