Rob*_*met 3 android parcelable android-fragments
我有一个带有SearchFragment和SearchResultFragment的MainActivity.
当用户按下搜索按钮时,我使用SearchParameters作为参数从SearchFragment回调到MainActivity.
在MainActivity上,我展示了一个LoadFragment并加载了结果.
当我收到结果时,我想将结果对象列表传递给SearchResultFragment.
做这个的最好方式是什么?
我知道你通常会这样做,如果它只是一个结果:
public static SearchResultFragment newInstance(SearchResult result) {
SearchResultFragment f = new SearchResultFragment();
Bundle args = new Bundle();
args.putInt("result", result); // Make result parceable
f.setArguments(args);
return f;
}
Run Code Online (Sandbox Code Playgroud)
但是如何使用对象列表执行此操作?
我能想到的另一个解决方案是在片段的onAttach方法中引用MainActivity,然后在MainActivity上调用方法getResults().
什么是最好的方式?
在大多数情况下,您可以避免传递复杂对象,但如果不能,则只需使用getter和setter.尝试这样的事情:
public static SearchResultFragment newInstance(SearchResult result) {
SearchResultFragment f = new SearchResultFragment();
f.setResult(result);
return f;
}
Run Code Online (Sandbox Code Playgroud)
但是尽量避免这种情况,用a传递数据Bundle是一个更好的解决方案.Parcelable尽可能使用.
| 归档时间: |
|
| 查看次数: |
5422 次 |
| 最近记录: |